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

jeff6868 geoffrey_klein at etu.u-bourgogne.fr
Mon Feb 9 16:47:16 CET 2015


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.



More information about the R-help mailing list