-----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
into long and then transform it back into wide format. The
at is:
df <- data.frame(ID1 = c(1,1,1,1,1,1,1,1,1), ID2 = c("A",
"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))
?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:
?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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