[R] string concatenate across rows of a matrix??
Albert Kim
alkim at u.washington.edu
Tue Sep 24 23:58:03 CEST 2002
Dalgaard and Plate suggestions both work. Thanks for the help.
I was tripped up by not realizing that the optional 'collapse' argument of
paste() must be specified. Otherwise, it defaults to NULL, which yields:
> apply(x2, 1, paste)
1 2 3 4
[1,] "a" "b" "c" "d"
[2,] "1" "2" "3" "4"
[3,] "k" "l" "m" "n"
-Al
On 24 Sep 2002, Peter Dalgaard BSA wrote:
> Tony Plate <tplate at blackmesacapital.com> writes:
>
> > > apply(format(x2), 1, paste, collapse=" ")
> > 1 2 3 4
> > "a 1 k" "b 2 l" "c 3 m" "d 4 n"
>
> ...or even (data frames only):
>
> > do.call("paste",x2)
> [1] "a 1 k" "b 2 l" "c 3 m" "d 4 n"
>
>
> >
> > At 12:45 PM 9/24/2002 -0700, Albert Kim wrote:
> >
> > >Is there a simple way to string-concatenate across the rows of a matrix or
> > >data.frame into a vector of strings? perhaps using paste() or
> > >something like it?
> > >
> > >That is, I want to map from something like x2 below:
> > >
> > > > x2 <- data.frame(C1 = letters[1:4], C3=1:4, C3=letters[11:14])
> > > > x2
> > > C1 C3 C3
> > > 1 a 1 k
> > > 2 b 2 l
> > > 3 c 3 m
> > > 4 d 4 n
> > >
> > >to something like:
> > >
> > > [1] "a 1 k"
> > > [2] "b 2 l"
> > > [3] "c 3 m"
> > > [4] "d 4 n"
> > >
> > >Sincerely,
> > >Al Kim
> > >Research Scientist
> > >Department of Psychology
> > >University of Washington, Box 351525
> > >Seattle, WA. 98195, USA
> > >E-Mail: alkim at u.washington.edu; Tel: (206)543-2395
> > >
> > >
> > >
> > >-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> > >r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> > >Send "info", "help", or "[un]subscribe"
> > >(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
> > > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
> >
> > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> > r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> > Send "info", "help", or "[un]subscribe"
> > (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
> > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
> >
>
> --
> O__ ---- Peter Dalgaard Blegdamsvej 3
> c/ /'_ --- Dept. of Biostatistics 2200 Cph. N
> (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918
> ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
>
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list