[R] Help on reshape function

Liaw, Andy andy_liaw at merck.com
Tue Mar 6 17:01:27 CET 2012


Just using the reshape() function in base R:

df.long = reshape(df, varying=list(names(df)[4:7]), direction="long")

This also gives two extra columns ("time" and "id") can can be dropped.

Andy 

> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of R. Michael Weylandt
> Sent: Tuesday, March 06, 2012 8:45 AM
> To: mails
> Cc: r-help at r-project.org
> Subject: Re: [R] Help on reshape function
> 
> library(reshape2)
> 
> melt(df, id.vars = c("ID1", "ID2", "ID3"))[, -4]
> # To drop an extraneous column (but you should take a look and see
> what it is for future reference)
> 
> Michael
> 
> On Tue, Mar 6, 2012 at 6:17 AM, mails <mails00000 at gmail.com> wrote:
> > Hello,
> >
> >
> > I am trying to reshape a data.frame in wide format into long format.
> > Although in the reshape R documentation
> > they programmer list some examples I am struggling to bring 
> my data.frame
> > into long and then transform it back into wide format. The 
> data.frame I look
> > at is:
> >
> >
> > df <- data.frame(ID1 = c(1,1,1,1,1,1,1,1,1), ID2 = c("A", 
> "A", "A", "B",
> > "B", "B", "C", "C", "C"),
> >
> >                                 ID3 = c("E", "E", "E", "E", 
> "E", "E", "E", "E", "E"),
> >
> >                                 X1 = c(1,4,3,5,2,4,6,4,2), 
> X2 = c(6,8,9,6,7,8,9,6,7),
> >
> >                                 X3 = c(7,6,7,5,6,5,6,7,5), 
> X4 = c(1,2,1,2,3,1,2,1,2))
> >
> >> df
> >  ID1 ID2 ID3 X1 X2 X3 X4
> > 1   1   A   E  1  6  7  1
> > 2   1   A   E  4  8  6  2
> > 3   1   A   E  3  9  7  1
> > 4   1   B   E  5  6  5  2
> > 5   1   B   E  2  7  6  3
> > 6   1   B   E  4  8  5  1
> > 7   1   C   E  6  9  6  2
> > 8   1   C   E  4  6  7  1
> > 9   1   C   E  2  7  5  2
> >
> > I want to use the reshape function to get the following result:
> >
> >> df
> >  ID1 ID2 ID3 X
> > 1   1   A   E  1
> > 2   1   A   E  4
> > 3   1   A   E  3
> > 4   1   B   E  5
> > 5   1   B   E  2
> > 6   1   B   E  4
> > 7   1   C   E  6
> > 8   1   C   E  4
> > 9   1   C   E  2
> >
> > 10   1   A   E  6
> > 11   1   A   E  8
> > 12   1   A   E  9
> > 13   1   B   E  6
> > 14   1   B   E  7
> > 15   1   B   E  8
> > 16   1   C   E  9
> > 17   1   C   E  6
> > 18   1   C   E  7
> >
> > 19   1   A   E  7
> > 20   1   A   E  6
> > 21   1   A   E  7
> > 22   1   B   E  5
> > 23   1   B   E  6
> > 24   1   B   E  5
> > 25   1   C   E  6
> > 26   1   C   E  7
> > 27   1   C   E  5
> >
> > 28   1   A   E  1
> > 29   1   A   E  2
> > 30   1   A   E  1
> > 31   1   B   E  2
> > 32   1   B   E  3
> > 33   1   B   E  1
> > 34   1   C   E  2
> > 35   1   C   E  1
> > 36   1   C   E  2
> >
> >
> > Can anyone help?
> >
> > Cheers
> >
> >
> >
> > --
> > View this message in context: 
> http://r.789695.n4.nabble.com/Help-on-reshape-function-tp44494
64p4449464.html
> > Sent from the R help mailing list archive at Nabble.com.
> >
> > ______________________________________________
> > 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.
> 
Notice:  This e-mail message, together with any attachme...{{dropped:11}}



More information about the R-help mailing list