Gundala Viswanath
2013-Feb-01 06:00 UTC
[R] Transforming 4x3 data frame into 2 column df in R
I have the following data frame:> foow 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 :> newdf1 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]]
Dear Gundala, Try as.data.frame.table(foo) HTH, Jorge.- On Fri, Feb 1, 2013 at 5:00 PM, Gundala Viswanath <> wrote:> 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@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. >[[alternative HTML version deleted]]
nalluri pratap
2013-Feb-01 13:45 UTC
[R] Transforming 4x3 data frame into 2 column df in R
newdf<-data.frame(k1=rep(row.names(foo),ncol(foo)),stack(foo)) --- On Fri, 1/2/13, Gundala Viswanath <gundalav@gmail.com> wrote: From: Gundala Viswanath <gundalav@gmail.com> Subject: [R] Transforming 4x3 data frame into 2 column df in R To: "r-help@stat.math.ethz.ch" <r-help@stat.math.ethz.ch> Date: Friday, 1 February, 2013, 11:30 AM I have the following data frame:> foow 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 :> newdf1 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@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. [[alternative HTML version deleted]]
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 :> newdf1? ? 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.