Bing Ho
2005-Sep-20 05:03 UTC
[R] Transform variable number of rows per subject to column variables?
Hello, I am very new to R, but I am having trouble with my dataset. I have a data frame where a subject has a variable number of multiple observations for each row, which I wish the transform these observations to column variables. An example of the data frame ID TEST.A TEST.B 1 10 1 1 13 2 1 11 1 2 15 2 2 17 3 And I wish to transform it to the following: ID TEST.A1 TEST.A2 TEST.A3 TEST.B1 TEST.B2 TEST.B3 1 10 13 11 1 2 1 2 15 17 NA 2 3 NA In other words, for the variable number of repeated follow up studies, a new column variable for each subject, but they are grouped by the original test. Thank you for any help - I'm realizing that I am a terrible programmer! Bing Ho
Gabor Grothendieck
2005-Sep-20 05:55 UTC
[R] Transform variable number of rows per subject to column variables?
On 9/20/05, Bing Ho <2bingho at stanford.edu> wrote:> Hello, > > I am very new to R, but I am having trouble with my dataset. > > I have a data frame where a subject has a variable number of multiple > observations for each row, which I wish the transform these > observations to column variables. > > An example of the data frame > ID TEST.A TEST.B > 1 10 1 > 1 13 2 > 1 11 1 > 2 15 2 > 2 17 3 > > And I wish to transform it to the following: > ID TEST.A1 TEST.A2 TEST.A3 TEST.B1 TEST.B2 TEST.B3 > 1 10 13 11 1 2 1 > 2 15 17 NA 2 3 NA > > In other words, for the variable number of repeated follow up > studies, a new column variable for each subject, but they are grouped > by the original test.First manufacture a "time" column and then use reshape: tt <- sequence(rle(DF$ID)$lengths) reshape(cbind(tt, DF), idvar = "ID", timevar = "tt", direction = "wide") Another possibility is to use the reshape package: library(reshape) DF.d <- deshape(cbind(tt, DF), id = 1:2) # same tt as above reshape(DF.d, ID ~ variable + tt)
Kevin Bartz
2005-Sep-20 16:52 UTC
[R] Transform variable number of rows per subject to column variables?
Hey Bing, Reshape's the ticker -- ?reshape. For example, reshape(myFrame, idvar = "ID", timevar = "TEST.A") should do most of the trick. Kevin -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Bing Ho Sent: Monday, September 19, 2005 10:04 PM To: r-help at stat.math.ethz.ch Subject: [R] Transform variable number of rows per subject to column variables? Hello, I am very new to R, but I am having trouble with my dataset. I have a data frame where a subject has a variable number of multiple observations for each row, which I wish the transform these observations to column variables. An example of the data frame ID TEST.A TEST.B 1 10 1 1 13 2 1 11 1 2 15 2 2 17 3 And I wish to transform it to the following: ID TEST.A1 TEST.A2 TEST.A3 TEST.B1 TEST.B2 TEST.B3 1 10 13 11 1 2 1 2 15 17 NA 2 3 NA In other words, for the variable number of repeated follow up studies, a new column variable for each subject, but they are grouped by the original test. Thank you for any help - I'm realizing that I am a terrible programmer! Bing Ho ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html