[R] Transpose Data Frame Question

Liaw, Andy andy_liaw at merck.com
Wed Sep 17 20:08:05 CEST 2003


Isn't this what you want?

> x <- data.frame(id=rep(1:2, each=2), test = rep(c("test1","test2"), 2), 
+                 score = c(120, 34, 132, 28))
> x
  id  test score
1  1 test1   120
2  1 test2    34
3  2 test1   132
4  2 test2    28
> reshape(x, timevar="test", direction="wide")
  id score.test1 score.test2
1  1         120          34
3  2         132          28


HTH,
Andy 

> -----Original Message-----
> From: Bruce Coate [mailto:coate111956 at yahoo.com] 
> Sent: Wednesday, September 17, 2003 1:30 PM
> To: r-help at stat.math.ethz.ch
> Subject: [R] Transpose Data Frame Question
> 
> 
> Hi,
>  
> I have a data.frame that has 3 columns (ID, Test, Result) and 
> looks like this
>  
> 1, Test1, 120
> 1, Test2, 34
> 2, Test1, 132
> 2, Test2, 28
> etc
>  
> I would like to turn it around so that it looks like this
>  
> 1, 120, 34
> 2, 132, 28
> etc
>  
> I have played around some with t and reshape, but with no success. 
> Any suggestions or hints would be greatly appreciated.
> Thanks,
> Bruce
> 
> 
> ---------------------------------
> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list 
> https://www.stat.math.ethz.ch/mailman/listinfo> /r-help
>




More information about the R-help mailing list