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

2015-02-10 Thread jeff6868
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.

__
R-help@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.


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

2015-02-09 Thread 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!




--
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@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.


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

2015-02-09 Thread Dennis Murphy
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_kl...@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@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.

__
R-help@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.


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

2015-02-09 Thread Erich Neuwirth

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




 On 09 Feb 2015, at 16:47, jeff6868 geoffrey_kl...@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))



signature.asc
Description: Message signed with OpenPGP using GPGMail
__
R-help@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.