jeff6868
2015-Feb-09 15:47 UTC
[R] transpose a data frame according to a specific variable
Dear R-users, I would like to transpose a large data.frame according to a specific column. Here's a reproductible example, it will be more understandable. At the moment, my data.frame looks like this example: DF <- data.frame(id=c("A","A","A","B","B","B","C","C","C"), Year=c(2001,2002,2003,2002,2003,2004,2000,2001,2002), Day=c(120,90,54,18,217,68,164,99,48)) I would like it being transformed to this (fake example again, still just for being understandable): finalDF <- data.frame(id=c("A","B","C"),"2000"=c(NA,NA,164),"2001"=c(120,NA,99), "2002"=c(90,18,48),"2003"=c(54,217,NA),"2004"=c(NA,68,NA)) Any ideas for doing this easily? I haven't found any good answer on the web. Thanks for the help! -- View this message in context: http://r.789695.n4.nabble.com/transpose-a-data-frame-according-to-a-specific-variable-tp4702971.html Sent from the R help mailing list archive at Nabble.com.
Dennis Murphy
2015-Feb-09 18:10 UTC
[R] transpose a data frame according to a specific variable
One way is to use the reshape2 package: library(reshape2) dcast(DF, id ~ Year, value.var = "Day") Dennis On Mon, Feb 9, 2015 at 7:47 AM, jeff6868 <geoffrey_klein at etu.u-bourgogne.fr> wrote:> Dear R-users, > > I would like to transpose a large data.frame according to a specific column. > Here's a reproductible example, it will be more understandable. > > At the moment, my data.frame looks like this example: > > DF <- data.frame(id=c("A","A","A","B","B","B","C","C","C"), > Year=c(2001,2002,2003,2002,2003,2004,2000,2001,2002), > Day=c(120,90,54,18,217,68,164,99,48)) > > I would like it being transformed to this (fake example again, still just > for being understandable): > > finalDF <- > data.frame(id=c("A","B","C"),"2000"=c(NA,NA,164),"2001"=c(120,NA,99), > "2002"=c(90,18,48),"2003"=c(54,217,NA),"2004"=c(NA,68,NA)) > > Any ideas for doing this easily? I haven't found any good answer on the web. > > Thanks for the help! > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/transpose-a-data-frame-according-to-a-specific-variable-tp4702971.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
Erich Neuwirth
2015-Feb-09 18:27 UTC
[R] transpose a data frame according to a specific variable
library(tidyr) spread(DF,Year,Day)> On 09 Feb 2015, at 16:47, jeff6868 <geoffrey_klein at etu.u-bourgogne.fr> wrote: > > finalDF <- > data.frame(id=c("A","B","C"),"2000"=c(NA,NA,164),"2001"=c(120,NA,99), > "2002"=c(90,18,48),"2003"=c(54,217,NA),"2004"=c(NA,68,NA))-------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 670 bytes Desc: Message signed with OpenPGP using GPGMail URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20150209/c8d279c3/attachment.bin>
jeff6868
2015-Feb-10 09:15 UTC
[R] transpose a data frame according to a specific variable
Both ways are doing well the job. Nice! Thanks again! -- View this message in context: http://r.789695.n4.nabble.com/transpose-a-data-frame-according-to-a-specific-variable-tp4702971p4703007.html Sent from the R help mailing list archive at Nabble.com.