[R] need help on melt/cast

David Winsemius dwinsemius at comcast.net
Thu Sep 22 18:44:19 CEST 2011


On Sep 22, 2011, at 12:28 PM, John Kane wrote:

> And I always have a problem with reshape().

Me too.

> Mind you I often have similar problems with melt()
>
Many fewer, though.

> Anyway with the data.frame xx, try
>
> melt(xx, id=c("ID"))

Just

newdf <-  melt(xx)   #  would have succeeded here.

--  
David
>
>
>
> --- On Thu, 9/22/11, Uwe Ligges <ligges at statistik.tu-dortmund.de>  
> wrote:
>
>> From: Uwe Ligges <ligges at statistik.tu-dortmund.de>
>> Subject: Re: [R] need help on melt/cast
>> To: "Eugene Kanshin" <kanshined1 at gmail.com>
>> Cc: r-help at r-project.org
>> Received: Thursday, September 22, 2011, 10:30 AM
>> I can never remember what melt, cast
>> and all that means, hence I simpy
>> use reshape() which does not even require any additional
>> package:
>>
>> reshape(dat, direction="long", idvar = "ID",
>>    varying=list(2:4), v.names="Value",
>> times=names(dat)[2:4])
>>
>> Uwe Ligges
>>
>>
>> On 22.09.2011 15:54, Eugene Kanshin wrote:
>>> Hello,
>>> I need to convert dataframe from:
>>>
>>>
>> ID   T0   T1   T2
>>> A    1     2
>>    3
>>> B    4     5
>>    6
>>> C    7     8
>>    9
>>>
>>> to:
>>>
>>> ID Variable Value
>>> A       T0
>>    1
>>> A       T1
>>    2
>>> A       T2
>>    3
>>> B       T0
>>    4
>>> B       T1
>>    5
>>> B       T2
>>    6
>>> C       T0
>>    7
>>> C       T1
>>    8
>>> C       T2
>>    9
>>>
>>> i tried to use melt cast but it gives me all the time
>> not exactly what I
>>> need.
>>> Thank you.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list