[R] Help with cast/reshape

Petr PIKAL petr.pikal at precheza.cz
Mon Oct 3 11:25:43 CEST 2011


Hi

> 
> I realize that this is terribly basic, but I just don't seem to see it 
at 
> this moment, so I would very much appreciate your help.
> 
> 
> How shall I transform this dataframe:
> 
> > df1
>   Name Index Value
> 1    a     1   0.1
> 2    a     2   0.2
> 3    a     3   0.3
> 4    a     4   0.4
> 5    b     1   2.1
> 6    b     2   2.2
> 7    b     3   2.3
> 8    b     4   2.4
> 
> 
> into this dataframe:
> 
> > df2
>     Index  a       b
> 1  1     0.1     2.1
> 2  2     0.2     2.2
> 3  3     0.3     2.3
> 4  4     0.4     2.4
> 

I have not seen an answer so I believe you look for:

cast(df1, Index~Name, value = "Value")

Regards
Petr


> 
> df1 = data.frame(c("a", "a", "a", "a", "b", "b", "b", "b"), 
c(1,2,3,4,1,2,
> 3,4), c(0.1, 0.2, 0.3, 0.4, 2.1, 2.2, 2.3, 2.4))
> colnames(df1) = c("Name", "Index", "Value")
> 
> df2 = data.frame(c(1,2,3,4), c(0.1, 0.2, 0.3, 0.4), c(2.1, 2.2, 2.3, 
2.4))
> colnames(df2) = c("Index", "a", "b")
> 
> 
> Thank you very much.
> 
> Dana
> 
> 
> ______________________________________________
> 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