[R] How to reshape wide format data.frame to long format?

Fredrik Karlsson dargosch at gmail.com
Thu Jan 20 16:51:55 CET 2011


Dear list,

I need to convert this data.frame

> names(codesM)
 [1] "key"        "AMR.pa1.M"  "AMR.pa2.M"  "AMR.pa3.M"  "AMR.pa4.M"
 [6] "AMR.pa5.M"  "AMR.pa6.M"  "AMR.pa7.M"  "AMR.pa8.M"  "AMR.pa9.M"
[11] "AMR.pa10.M" "AMR.ta1.M"  "AMR.ta2.M"  "AMR.ta3.M"  "AMR.ta4.M"
[16] "AMR.ta5.M"  "AMR.ta6.M"  "AMR.ta7.M"  "AMR.ta8.M"  "AMR.ta9.M"
[21] "AMR.ta10.M" "AMR.ka1.M"  "AMR.ka2.M"  "AMR.ka3.M"  "AMR.ka4.M"
[26] "AMR.ka5.M"  "AMR.ka6.M"  "AMR.ka7.M"  "AMR.ka8.M"  "AMR.ka9.M"
[31] "AMR.ka10.M" "SMR.pa1.M"  "SMR.pa2.M"  "SMR.pa3.M"  "SMR.pa4.M"
[36] "SMR.pa5.M"  "SMR.pa6.M"  "SMR.pa7.M"  "SMR.pa8.M"  "SMR.pa9.M"
[41] "SMR.pa10.M" "SMR.ta1.M"  "SMR.ta2.M"  "SMR.ta3.M"  "SMR.ta4.M"
[46] "SMR.ta5.M"  "SMR.ta6.M"  "SMR.ta7.M"  "SMR.ta8.M"  "SMR.ta9.M"
[51] "SMR.ta10.M" "SMR.ka1.M"  "SMR.ka2.M"  "SMR.ka3.M"  "SMR.ka4.M"
[56] "SMR.ka5.M"  "SMR.ka6.M"  "SMR.ka7.M"  "SMR.ka8.M"  "SMR.ka9.M"
[61] "SMR.ka10.M"
> dim(codesM)
[1] 42 61

into a 3 x  2501 data.frame where the "key" variable is kept, the
values in columns 2-61 above is inserted into a "values" column and
the name of the column is inserted in a third column ("variable"
perhaps).

Like

key                         variable              value
POSTOFF_1_1    AMR.pa1.M       5
POSTOFF_1_1    AMR.pa2.M       3
....

I think I should be able to do this using the "reshape" function, but
I cannot get it to work. I think I need some help to understand
this...


(If I could split the "variable" into three separate columns splitting
by ".", that would be even better.)

I appreciate all the help I could get.

/Fredrik

-- 
"Life is like a trumpet - if you don't put anything into it, you don't
get anything out of it."



More information about the R-help mailing list