Hi, I have a data.frame that has 3 columns (ID, Test, Result) and looks like this 1, Test1, 120 1, Test2, 34 2, Test1, 132 2, Test2, 28 etc I would like to turn it around so that it looks like this 1, 120, 34 2, 132, 28 etc I have played around some with t and reshape, but with no success. Any suggestions or hints would be greatly appreciated. Thanks, Bruce --------------------------------- [[alternative HTML version deleted]]
Here is a brute force way: mm <- NULL for(i in unique(ID)){ zz <- data.frame[ID==i, 3] mm <- rbind(mm, c(i, zz)) } It will work as long as you have the same number of tests for each ID. If not, you would need to pad shorter zz vectors with NAs. Cheers, Andy __________________________________ Andy Jaworski Engineering Systems Technology Center 3M Center, 518-1-01 St. Paul, MN 55144-1000 ----- E-mail: apjaworski at mmm.com Tel: (651) 733-6092 Fax: (651) 736-3122 |---------+--------------------------------> | | Bruce Coate | | | <coate111956 at yahoo.co| | | m> | | | Sent by: | | | r-help-bounces at stat.m| | | ath.ethz.ch | | | | | | | | | 09/17/2003 12:30 | | | | |---------+--------------------------------> >-----------------------------------------------------------------------------------------------------------------------------| | | | To: r-help at stat.math.ethz.ch | | cc: | | Subject: [R] Transpose Data Frame Question | >-----------------------------------------------------------------------------------------------------------------------------| Hi, I have a data.frame that has 3 columns (ID, Test, Result) and looks like this 1, Test1, 120 1, Test2, 34 2, Test1, 132 2, Test2, 28 etc I would like to turn it around so that it looks like this 1, 120, 34 2, 132, 28 etc I have played around some with t and reshape, but with no success. Any suggestions or hints would be greatly appreciated. Thanks, Bruce --------------------------------- [[alternative HTML version deleted]] ______________________________________________ R-help at stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Isn't this what you want?> x <- data.frame(id=rep(1:2, each=2), test = rep(c("test1","test2"), 2),+ score = c(120, 34, 132, 28))> xid test score 1 1 test1 120 2 1 test2 34 3 2 test1 132 4 2 test2 28> reshape(x, timevar="test", direction="wide")id score.test1 score.test2 1 1 120 34 3 2 132 28 HTH, Andy> -----Original Message----- > From: Bruce Coate [mailto:coate111956 at yahoo.com] > Sent: Wednesday, September 17, 2003 1:30 PM > To: r-help at stat.math.ethz.ch > Subject: [R] Transpose Data Frame Question > > > Hi, > > I have a data.frame that has 3 columns (ID, Test, Result) and > looks like this > > 1, Test1, 120 > 1, Test2, 34 > 2, Test1, 132 > 2, Test2, 28 > etc > > I would like to turn it around so that it looks like this > > 1, 120, 34 > 2, 132, 28 > etc > > I have played around some with t and reshape, but with no success. > Any suggestions or hints would be greatly appreciated. > Thanks, > Bruce > > > --------------------------------- > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo> /r-help >