[R] Transforming 4x3 data frame into 2 column df in R

arun smartpink111 at yahoo.com
Fri Feb 1 19:24:02 CET 2013


Hi,
library(reshape)
foo$id<-row.names(foo)
 melt(foo,id.var="id")
   id variable      value
#1   n        w 1.51550092
#2   q        w 0.09977303
#3   r        w 1.17083866
#4   n        x 1.43375720
#5   q        x 0.81737610
#6   r        x 1.24693470
#7   n        y 1.27916240
#8   q        y 1.61234020
#9   r        y 0.87121350
#10  n        z 1.17712300
#11  q        z 0.15107370
#12  r        z 0.84880290
A.K.




----- Original Message -----
From: Gundala Viswanath <gundalav at gmail.com>
To: "r-help at stat.math.ethz.ch" <r-help at stat.math.ethz.ch>
Cc: 
Sent: Friday, February 1, 2013 1:00 AM
Subject: [R] Transforming 4x3 data frame into 2 column df in R

I have the following data frame:

> foo
           w         x         y         z
n 1.51550092 1.4337572 1.2791624 1.1771230
q 0.09977303 0.8173761 1.6123402 0.1510737
r 1.17083866 1.2469347 0.8712135 0.8488029

What I want to do is to change it into :

> newdf
1     n    w 1.51550092
2     q   w 0.09977303
3     r   w 1.17083866
4     n    x 1.43375725
5     q   x 0.81737606
6     r   x 1.24693468
7     n   y 1.27916241
8     q   y 1.61234016
9     r   y 0.87121353
10   n    z 1.17712302
11   q   z 0.15107369
12   r    z 0.84880292

Whtat's the way to do it?

- G.V.

    [[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.




More information about the R-help mailing list