[R] Converting data.frame from long to wide format
Jonathan Christensen
dzhonatan at gmail.com
Thu Dec 9 03:33:06 CET 2010
Matt,
library(reshape2)
wide.df <- dcast(df, y ~ x)
Works great for me.
Jonathan
On Wed, Dec 8, 2010 at 7:26 PM, Matthew Pettis <matthew.pettis at gmail.com> wrote:
> Hi,
>
> I was wondering if there is an easy way that I am missing for turning a long
> dataframe into a wide one. Below is sample code that will make what I have
> and, in comments, the form of what I want:
>
> # Have: dataframe like 'df'
> df <- expand.grid( x=LETTERS[1:3], y=LETTERS[4:6])
> df$z <- letters[1:length(df[,1])]
>
> # Want: data.frame that has following form:
> # A B C
> # D a b c
> # E d e f
> # F g h i
>
>
> I looked at 'xtabs' and 'cast' from reshape/reshape2, but unless I'm
> misunderstanding something, these will work only for the 'z' column being
> numeric, not textual. Is there an easy way to do this with 'z' being
> textual rather than numeric?
>
> tia,
> Matt
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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