[R] Reshaping a data frame

Gabor Grothendieck ggrothendieck at myway.com
Sat Feb 26 19:11:23 CET 2005


Matthew Pharr <matthewpharr <at> gmail.com> writes:

: 
: I am new to R. Having come from SPSS and SAS to this program, I know
: that my problem is
: reshaping from long to wide. How would I go about reshaping data frame
: (A) so that
: information for each record is contained on one row. I am aware of the
: R reshape
: function, but I am uncertain of how to instruct R to reshape the data
: set because I
: need to move var1 (var1 is a survey question) out wide while placing
: the values of var2 (var2 is the answer to the survey question) under
: the correct survey question. Thank you for any help provided.
: 
: *Data Frame (A)
: 
: id        var1               var2
: 
: 12345  gender_m      1
: 12345  age_22          1
: 12345  car_benz       1
: 12345  reader           28
: 23456  gender_f        1
: 23456  age_35          1
: 23456  workwk         40
: 23456  reader           30
: 23456  kid_0_3         1
: 34567  gender_m      1
: 34567  age_45          1
: 
: *Data Frame (B)
:   id     gender_m  age_22  car_benz  reader 
: 12345       1             1           1           28

Another possibility is to use xtabs. The formula specifies
that var2 is to be the table entry while the rows and columns
are to be id and var1:

R> xtabs(var2 ~ id + var1, A)
       var1
id      age_22 age_35 age_45 car_benz gender_f gender_m kid_0_3 reader workwk
  12345      1      0      0        1        0        1       0     28      0
  23456      0      1      0        0        1        0       1     30     40
  34567      0      0      1        0        0        1       0      0      0




More information about the R-help mailing list