[R] need help on melt/cast

Dimitris Rizopoulos d.rizopoulos at erasmusmc.nl
Thu Sep 22 16:24:13 CEST 2011


You can use function reshape(), e.g.,

DF <- data.frame(ID = LETTERS[1:3],
     T0 = c(1,4,7), T1 = c(2,5,8), T2 = c(3,6,9))

DF.new <- reshape(DF, idvar = "ID", direction = "long",
     varying = list(2:4), times = names(DF[-1]))
DF.new
DF.new[order(DF.new$ID), ]


I hope it helps.

Best,
Dimitris


On 9/22/2011 3:54 PM, 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.
>

-- 
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014
Web: http://www.erasmusmc.nl/biostatistiek/



More information about the R-help mailing list