[R] how to convert data from long to wide format ?

jim holtman jholtman at gmail.com
Thu Oct 30 13:37:40 CET 2008


You can look at the reshape package:

> x
  X Y  V3  V4
1 1 A 0.5 1.2
2 1 B 0.2 1.4
3 2 A 0.1 0.9
> z <- melt(x[,-3])  # ignore V3
Using X, Y as id variables
> z
  X Y variable value
1 1 A       V4   1.2
2 1 B       V4   1.4
3 2 A       V4   0.9
> cast(z, X ~ Y, sum)
  X   A   B
1 1 1.2 1.4
2 2 0.9  NA
>


On Thu, Oct 30, 2008 at 7:29 AM, Daren Tan <daren76 at hotmail.com> wrote:
>
> Given a dataframe m
>> m
>   X Y V3 V4
> 1 1 A 0.5 1.2
> 2 1 B 0.2 1.4
> 3 2 A 0.1 0.9
>
> How do I convert m to this with V4 as the cell values ?
>
>   A    B
> 1 1.2 1.4
> 2 0.9 NA
>
> ______________________________________________
> 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?



More information about the R-help mailing list