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]]
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]] > > ______________________________________________ > 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. >-- 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/
?reshape ________________________________________ From: r-help-bounces at r-project.org [r-help-bounces at r-project.org] On Behalf Of Eugene Kanshin [kanshined1 at gmail.com] Sent: Thursday, September 22, 2011 9:54 AM To: r-help at r-project.org 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]] ______________________________________________ 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.
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]] > > ______________________________________________ > 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.
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 <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. > > > > ??? [[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. > > ______________________________________________ > 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. >
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
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 Liggeswww 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 whatI> > need. > > Thank you. > > > > [[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 guidehttp://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 guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
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]] > > ______________________________________________ > 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.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