Hi R, Can I transpose a data frame by a particular group variable? For example: d=data.frame(group=c(1,1,2,2,2),val=c(6,4,6,3,5)) And my output should be: data.frame(group=c(1,2),v1=c(6,6),v2=c(4,3),v3=c(NA,5)) Many thanks, Shubha This e-mail may contain confidential and/or privileged i...{{dropped:13}}
try this: d <- data.frame(group = c(1,1,2,2,2), val = c(6,4,6,3,5)) d$time <- unlist(tapply(d$group, d$group, function (x) seq(1, len = length(x)))) reshape(d, idvar = "group", direction = "wide") I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student 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://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Shubha Vishwanath Karanth" <shubhak at ambaresearch.com> To: <r-help at stat.math.ethz.ch> Sent: Thursday, February 14, 2008 3:22 PM Subject: [R] Transposing by a group variable> Hi R, > > > > Can I transpose a data frame by a particular group variable? > > > > For example: > > d=data.frame(group=c(1,1,2,2,2),val=c(6,4,6,3,5)) > > And my output should be: > > data.frame(group=c(1,2),v1=c(6,6),v2=c(4,3),v3=c(NA,5)) > > > > Many thanks, > > Shubha > > This e-mail may contain confidential and/or privileged > i...{{dropped:13}} > > ______________________________________________ > 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
Try also: reshape(cbind(d, time=unlist(sapply(table(d$group), seq))), idvar="group", direction="wide") On 14/02/2008, Shubha Vishwanath Karanth <shubhak at ambaresearch.com> wrote:> Hi R, > > > > Can I transpose a data frame by a particular group variable? > > > > For example: > > d=data.frame(group=c(1,1,2,2,2),val=c(6,4,6,3,5)) > > And my output should be: > > data.frame(group=c(1,2),v1=c(6,6),v2=c(4,3),v3=c(NA,5)) > > > > Many thanks, > > Shubha > > This e-mail may contain confidential and/or privileged i...{{dropped:13}} > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
How do we know what value corresponds to what new variable? library(reshape) mm <- melt(d, id="group") cast(mm, group~value) will give you something but not quite what you want. --- Shubha Vishwanath Karanth <shubhak at ambaresearch.com> wrote:> Hi R, > > > > Can I transpose a data frame by a particular group > variable? > > > > For example: > > d=data.frame(group=c(1,1,2,2,2),val=c(6,4,6,3,5)) > > And my output should be: > >data.frame(group=c(1,2),v1=c(6,6),v2=c(4,3),v3=c(NA,5))> > > > Many thanks, > > Shubha > > This e-mail may contain confidential and/or > privileged i...{{dropped:13}} > > ______________________________________________ > 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. >