[R] reshape from wide to long

Gabor Grothendieck ggrothendieck at gmail.com
Thu Feb 26 06:10:20 CET 2009


Try:

reshape(x, dir = "long", idvar = 1, times = cn[-1], varying =
list(cn[-1]), v.names = "X", timevar = "Name")


On Wed, Feb 25, 2009 at 11:01 PM, Juliet Hannah <juliet.hannah at gmail.com> wrote:
> Out of curiosity, what would be a good way to do this without using
> the reshape package? Thanks!
>
> On Wed, Feb 25, 2009 at 8:04 PM, jim holtman <jholtman at gmail.com> wrote:
>> You can use the 'reshape' package:
>>
>>> x <- read.table(textConnection(" Grp        X0        X3               X6           X12          X25     X50
>> +    1    C 0.5326517 0.6930942  0.9403883  1.157571  2.483117  2.809012
>> + 2    C 0.4715917 0.8613200  2.0706117  2.937632  7.530960  7.120678
>> + 3    C 0.5021217 0.7772071  1.5055000  2.047601  5.007038  4.964845
>> + 4    E 0.5845200 0.6439033  0.8221400  2.148100  1.862153 12.412500
>> + 5    E 0.2985675 0.6561142  1.0987058  2.842180  4.057899  2.319930
>> + 6    E 1.7854500 1.9813878  1.8340700  3.507966  1.120367  2.603823
>> + 7    E 6.4370500 2.2302000 18.5271000 17.139600 22.978400  7.116638
>> + 8    E 2.2763969 1.3779013  5.5705040  6.409461  7.504704  6.113223
>> + 9    T 0.9649327 0.9825045  1.0099582  1.713536  4.652827  3.877736
>> + 10   T 2.0774727 2.4916545  4.0087455  8.183382 26.381091 26.279636
>> + 11   T 2.2527000 2.6311091  3.1711727  4.316418  9.255364 28.771473
>> + 12   T 1.3989636 1.7640273  2.5264636  4.712164 23.606545 32.030000
>> + 13   T 1.4211167 1.5128417  1.7613583  2.351858  8.411725 55.448000
>> + 14   T 1.5992167 2.2668167  2.5927667  2.947150  6.025300 24.984767
>> + 15   T 2.9468333 3.3383667  3.1090667  3.244675  3.037425  2.679587
>> + 16   T 1.0867725 1.7652583  2.0307008  5.201625  8.279975 14.693583
>> + 17   T 1.7185010 2.0940723  2.5262791  4.083851 11.206282
>> 23.595598"), header=TRUE)
>>> closeAllConnections()
>>> require(reshape)
>> Loading required package: reshape
>> Loading required package: plyr
>>> x.m <- melt(x)
>> Using Grp as id variables
>>>
>>> head(x.m)
>>  Grp variable     value
>> 1   C       X0 0.5326517
>> 2   C       X0 0.4715917
>> 3   C       X0 0.5021217
>> 4   E       X0 0.5845200
>> 5   E       X0 0.2985675
>> 6   E       X0 1.7854500
>>>
>>
>>
>> On Wed, Feb 25, 2009 at 4:59 PM, Veerappa Chetty <chettyvk at gmail.com> wrote:
>>> Hi,I would like to reshape the following "wide" data set to "long" form. I
>>> would appreciate help with the correct code for "reshape". I tried a few
>>> unsuccessfully.
>>> Thanks.
>>> Chetty
>>> __________________________________________________
>>> dat.1
>>>
>>>  Grp        X0        X3               X6           X12          X25
>>>    X501    C 0.5326517 0.6930942  0.9403883  1.157571  2.483117  2.809012
>>> 2    C 0.4715917 0.8613200  2.0706117  2.937632  7.530960  7.120678
>>> 3    C 0.5021217 0.7772071  1.5055000  2.047601  5.007038  4.964845
>>> 4    E 0.5845200 0.6439033  0.8221400  2.148100  1.862153 12.412500
>>> 5    E 0.2985675 0.6561142  1.0987058  2.842180  4.057899  2.319930
>>> 6    E 1.7854500 1.9813878  1.8340700  3.507966  1.120367  2.603823
>>> 7    E 6.4370500 2.2302000 18.5271000 17.139600 22.978400  7.116638
>>> 8    E 2.2763969 1.3779013  5.5705040  6.409461  7.504704  6.113223
>>> 9    T 0.9649327 0.9825045  1.0099582  1.713536  4.652827  3.877736
>>> 10   T 2.0774727 2.4916545  4.0087455  8.183382 26.381091 26.279636
>>> 11   T 2.2527000 2.6311091  3.1711727  4.316418  9.255364 28.771473
>>> 12   T 1.3989636 1.7640273  2.5264636  4.712164 23.606545 32.030000
>>> 13   T 1.4211167 1.5128417  1.7613583  2.351858  8.411725 55.448000
>>> 14   T 1.5992167 2.2668167  2.5927667  2.947150  6.025300 24.984767
>>> 15   T 2.9468333 3.3383667  3.1090667  3.244675  3.037425  2.679587
>>> 16   T 1.0867725 1.7652583  2.0307008  5.201625  8.279975 14.693583
>>> 17   T 1.7185010 2.0940723  2.5262791  4.083851 11.206282 23.595598
>>>
>>>
>>> --
>>> Professor of Family Medicine
>>> Boston University
>>> Tel: 617-414-6221, Fax:617-414-3345
>>> emails: chettyvk at gmail.com,vchetty at bu.edu
>>>
>>>        [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
>>
>>
>> --
>> Jim Holtman
>> Cincinnati, OH
>> +1 513 646 9390
>>
>> What is the problem that you are trying to solve?
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>




More information about the R-help mailing list