transpose a data frame according to a specific variable

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

transpose a data frame according to a specific variable

jeff6868
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!
Reply | Threaded
Open this post in threaded view
|

Re: transpose a data frame according to a specific variable

djmuseR
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
<[hidden email]> 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.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: transpose a data frame according to a specific variable

Erich Neuwirth
In reply to this post by jeff6868

library(tidyr)
spread(DF,Year,Day)




> On 09 Feb 2015, at 16:47, jeff6868 <[hidden email]> 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))


______________________________________________
[hidden email] 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.

signature.asc (684 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: transpose a data frame according to a specific variable

jeff6868
In reply to this post by jeff6868
Both ways are doing well the job. Nice!
Thanks again!