need help on melt/cast

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

need help on melt/cast

Eugene Kanshin
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.

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: need help on melt/cast

D. Rizopoulos
You can use function reshape(), e.g.,

DF <- data.frame(ID = LETTERS[1:3],
     T0 = c(1,4,7), T1 = c(2,5,8), T2 = c(3,6,9))

DF.new <- reshape(DF, idvar = "ID", direction = "long",
     varying = list(2:4), times = names(DF[-1]))
DF.new
DF.new[order(DF.new$ID), ]


I hope it helps.

Best,
Dimitris


On 9/22/2011 3:54 PM, 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.
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.
>

--
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014
Web: http://www.erasmusmc.nl/biostatistiek/

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: need help on melt/cast

Doran, Harold
In reply to this post by Eugene Kanshin
?reshape
________________________________________
From: [hidden email] [[hidden email]] On Behalf Of Eugene Kanshin [[hidden email]]
Sent: Thursday, September 22, 2011 9:54 AM
To: [hidden email]
Subject: [R] need help on melt/cast

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.

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: need help on melt/cast

Uwe Ligges-3
In reply to this post by Eugene Kanshin
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.
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: need help on melt/cast

John Kane-2
And I always have a problem with reshape(). Mind you I often have similar problems with melt()

Anyway with the data.frame xx, try

 melt(xx, id=c("ID"))



--- On Thu, 9/22/11, Uwe Ligges <[hidden email]> wrote:

> From: Uwe Ligges <[hidden email]>
> Subject: Re: [R] need help on melt/cast
> To: "Eugene Kanshin" <[hidden email]>
> Cc: [hidden email]
> 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.
> >
> >     [[alternative HTML version
> deleted]]
> >
> > ______________________________________________
> > [hidden email]
> 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.
>
> ______________________________________________
> [hidden email]
> 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: need help on melt/cast

David Winsemius

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 <[hidden email]>  
> wrote:
>
>> From: Uwe Ligges <[hidden email]>
>> Subject: Re: [R] need help on melt/cast
>> To: "Eugene Kanshin" <[hidden email]>
>> Cc: [hidden email]
>> 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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: need help on melt/cast

PIKAL Petr
In reply to this post by Uwe Ligges-3
Hi


>
> 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

www
  ID T0 T1 T2
1  A  1  2  3
2  B  4  5  6
3  C  7  8  9
melt(www)

Using ID as id variables
  ID variable value
1  A       T0     1
2  B       T0     4
3  C       T0     7
4  A       T1     2
5  B       T1     5
6  C       T1     8
7  A       T2     3
8  B       T2     6
9  C       T2     9

AFAIK melt does exactly what OP wanted only sorting of columns is
different. So

mmm[order(mmm$ID),]
  ID variable value
1  A       T0     1
4  A       T1     2
7  A       T2     3
2  B       T0     4
5  B       T1     5
8  B       T2     6
3  C       T0     7
6  C       T1     8
9  C       T2     9

Therefore simple ordering makes it.

Regards
Petr



>
>
> 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.
> >
> >    [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: need help on melt/cast

Paul Hiemstra-2
In reply to this post by Eugene Kanshin
On 09/22/2011 01:54 PM, 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.
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.

Hi,

I see you already got your answer, but maybe than for future
questions... You mention, "I tried to use melt and cast" but you do not
provide example code that shows what you tried. This is important for us
to understand where you reasoning about melt and cast goes wrong.
Especially because a simple (as David mentioned):

melt(dat)

got you the right answer.

good luck,
Paul

--
Paul Hiemstra, Ph.D.
Global Climate Division
Royal Netherlands Meteorological Institute (KNMI)
Wilhelminalaan 10 | 3732 GK | De Bilt | Kamer B 3.39
P.O. Box 201 | 3730 AE | De Bilt
tel: +31 30 2206 494

http://intamap.geo.uu.nl/~paul
http://nl.linkedin.com/pub/paul-hiemstra/20/30b/770

______________________________________________
[hidden email] 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.