[R] Help with package reshape, wide to long

Chuck Cleland ccleland at optonline.net
Thu Feb 7 19:25:47 CET 2008


On 2/7/2008 11:40 AM, Don MacQueen wrote:
> Hello,
> 
> I am having difficulty figuring out how to use functions in the 
> reshape package to perform a wide to long transformation
> 
> I have a "wide" dataframe whose columns are like this example:
> 
>    id1 id2 subject treat height weight age
> 
> id1 and id2 are unique for each row
> subject and treat are not unique for each row
> height, weight, and age are different types of measurements made on 
> each unique combination of subject and treatment
> 
> I want to reshape to a long format which will look like this:
> 
>   id1 id2 subject treat measurement.type value
> 
> where
> 
> measurement.type identifies the type of measurement, i.e. 'height', 
> 'weight', 'age'
> value contains the values of those measurements
> and the other variables are replicated as necessary

   Does something like this work for you?

df <- data.frame(id1 = c(1,2,3),
                  id2 = c(4,5,6),
                  subject = runif(3),
                  treat = runif(3),
                  height = runif(3),
                  weight = runif(3),
                  age = runif(3))

library(reshape)

melt(df, measure.var = c("height","weight","age"))

   id1 id2   subject      treat variable      value
1   1   4 0.4736760 0.02221288   height 0.09290354
2   2   5 0.1708840 0.84465476   height 0.70660286
3   3   6 0.6955085 0.56084004   height 0.52761841
4   1   4 0.4736760 0.02221288   weight 0.08853412
5   2   5 0.1708840 0.84465476   weight 0.85745906
6   3   6 0.6955085 0.56084004   weight 0.29902097
7   1   4 0.4736760 0.02221288      age 0.91842595
8   2   5 0.1708840 0.84465476      age 0.93592277
9   3   6 0.6955085 0.56084004      age 0.01587108

> To put it another way, can I use reshape() to transform my original 
> dataframe, which has 45 rows, into a "long" form that has 3*45  = 135 
> rows:  45 rows for height, 45 for weight, 45 for age, with the other 
> variables carried along as is within each set of 45, and a new 
> variable that identifies the type of measurement in each row of the 
> long form, i.e., 'height' in 45 rows, 'weight' in 45 rows, and 'age' 
> in 45 rows.
> 
> I know it's not difficult to do this with explicit looping, using 
> rbind(), but it seems like reshape() is a natural tool. But I'm not 
> getting it, so I'd appreciate some help.
> 
> (in case anyone is wondering whether it makes sense to do this, 
> height, weight, and age are just examples; it makes more sense with 
> my actual measurements)
> 
> Thanks
> -Don

-- 
Chuck Cleland, Ph.D.
NDRI, Inc.
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 512-0171 (M, W, F)
fax: (917) 438-0894



More information about the R-help mailing list