[R] transpose a data frame according to a specific variable

Dennis Murphy djmuser at gmail.com
Mon Feb 9 19:10:29 CET 2015


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.



More information about the R-help mailing list