[R] Reshaping data from wide to tall format for multilevel modeling

dadrivr dadrivr at gmail.com
Wed Sep 7 16:02:32 CEST 2011


I'm trying to reshape my data set from wide to tall format for multilevel
modeling.  Unfortunately, the function I typically use (make.univ from the
multilevel package) does not appear to work with unbalanced data frames,
which is what I'm dealing with.

Below is an example of the columns of a data frame similar to what I'm
working with:
ID  a1  a2  a4  b2  b3  b4  b5  b6

Below is what I want the columns to be after reshaping the data to long
ID  a  b time

Here is an example data frame that I want to reshape:
ID <- c(1,2,3)
a1 <- c(NA, rnorm(2))
a2 <- c(NA, rnorm(1), NA)
a4 <- c(NA, rnorm(2))
b2 <- c(rnorm(2), NA)
b3 <- rnorm(3)
b4 <- NA
b5 <- rnorm(3)
b6 <- rnorm(3)
mydata <- as.data.frame(cbind(ID,a1,a2,a4,b2,b3,b4,b5,b6))

What is the best way to do this efficiently with MANY variables with widely
differing time ranges?  Note that I will have to manually enter the time for
a given measurement because in the wide format, the time is in the variable
names.  By the way, I have a fairly large data set, with some variables
occurring at 2 time points and other variables occurring at 20 time points. 
Thanks for your help!

View this message in context: http://r.789695.n4.nabble.com/Reshaping-data-from-wide-to-tall-format-for-multilevel-modeling-tp3796168p3796168.html
Sent from the R help mailing list archive at Nabble.com.

More information about the R-help mailing list