Re: [R] column wise paste of data.frames

From: Dimitris Rizopoulos <dimitris.rizopoulos_at_med.kuleuven.be>
Date: Fri, 18 Jul 2008 10:05:07 +0200

try this:

data("Orthodont", package = "nlme")
metaFields <- c("Subject", "Sex")
Orthodont$ID <- do.call("paste", c(Orthodont[metaFields], sep = "/"))

I hope it helps.

Best,
Dimitris



Dimitris Rizopoulos
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium

Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
     http://perswww.kuleuven.be/dimitris_rizopoulos/


> Hi everybody!
>
> I'm sure that I overlook something and feel quite stupid to ask, but
> I
> have not found an easy solution to the following problem: Take e.g.
> the
> Orthodont data from the nlme package:
>
>> head(Orthodont)
> Grouped Data: distance ~ age | Subject
> distance age Subject Sex
> 1 26.0 8 M01 Male
> 2 25.0 10 M01 Male
> 3 29.0 12 M01 Male
> 4 31.0 14 M01 Male
> 5 21.5 8 M02 Male
> 6 22.5 10 M02 Male
>
> Suppose now, that I have a vector which contains the names of the
> "meta"-fields in this data-frame:
>
> metaFields <- c("Subject", "Sex")
>
> I now would like to form a new column "ID" which consists of the
> columns
> in metaFields joined like the following:
>
>> head(Orthodont)
> Grouped Data: distance ~ age | Subject
> distance age Subject Sex ID
> 1 26.0 8 M01 Male M01/Male
> 2 25.0 10 M01 Male M01/Male
> 3 29.0 12 M01 Male M01/Male
> 4 31.0 14 M01 Male M01/Male
> 5 21.5 8 M02 Male M02/Male
> 6 22.5 10 M02 Male M02/Male
>
> Any paste commands I tried so far ´╗┐failed, e.g.:
>
> Orthodont$ID <- paste(Orthodont[,metaFields], sep="/")
>
> is simply wrong. Ok, I could add the columns within a loop to ID,
> but
> that is not very elegant. Instead, what I need is a command that in
> effect produces the output of
>
> Orthodont$ID <- paste(Orthodont[,metaFields[1]],
> Orthodont[,metaFields[2]] , sep="/")
>
> There should be some way to do it in R - can someone please
> enlighten
> me? Thanks a lot in advance.
>
> Greetings,
>
> Sebastian
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Fri 18 Jul 2008 - 08:07:30 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 18 Jul 2008 - 09:31:57 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive