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
On Sat, 26 Feb 2005, Matthew Pharr wrote:> 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.You want var1 to be the "timevar":> reshape(a,direction="wide",timevar="var1",idvar="id")id var2.gender_m var2.age_22 var2.car_benz var2.reader var2.gender_f 1 12345 1 1 1 28 NA 5 23456 NA NA NA 30 1 10 34567 1 NA NA NA NA var2.age_35 var2.workwk var2.kid_0_3 var2.age_45 1 NA NA NA NA 5 1 40 1 NA 10 NA NA NA 1 -thomas
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