[R] need help on melt/cast

Petr PIKAL petr.pikal at precheza.cz
Fri Sep 23 09:08:02 CEST 2011


Hi


> 
> I can never remember what melt, cast and all that means, hence I simpy 
> use reshape() which does not even require any additional package:
> 
> reshape(dat, direction="long", idvar = "ID",
>    varying=list(2:4), v.names="Value", times=names(dat)[2:4])
> 
> Uwe Ligges

www
  ID T0 T1 T2
1  A  1  2  3
2  B  4  5  6
3  C  7  8  9
melt(www)

Using ID as id variables
  ID variable value
1  A       T0     1
2  B       T0     4
3  C       T0     7
4  A       T1     2
5  B       T1     5
6  C       T1     8
7  A       T2     3
8  B       T2     6
9  C       T2     9

AFAIK melt does exactly what OP wanted only sorting of columns is 
different. So 

mmm[order(mmm$ID),]
  ID variable value
1  A       T0     1
4  A       T1     2
7  A       T2     3
2  B       T0     4
5  B       T1     5
8  B       T2     6
3  C       T0     7
6  C       T1     8
9  C       T2     9

Therefore simple ordering makes it.

Regards
Petr



> 
> 
> On 22.09.2011 15:54, Eugene Kanshin wrote:
> > Hello,
> > I need to convert dataframe from:
> >
> > ID   T0   T1   T2
> > A    1     2     3
> > B    4     5     6
> > C    7     8     9
> >
> > to:
> >
> > ID Variable Value
> > A       T0       1
> > A       T1       2
> > A       T2       3
> > B       T0       4
> > B       T1       5
> > B       T2       6
> > C       T0       7
> > C       T1       8
> > C       T2       9
> >
> > i tried to use melt cast but it gives me all the time not exactly what 
I
> > need.
> > Thank you.
> >
> >    [[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.
> 
> ______________________________________________
> 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