Hello Experts, how do I reformat a data frame in the way described below: df1: ID desc resist thick temp 1 4711 100 5 20 2 4712 101 4 21 3 4711 99 3 19 4 4712 98 7 22 TO df2: id desc Param Value 1 4711 resist 100 1 4711 Thick 5 1 4711 temp 20 2 4712 resist 101 2 4712 Thick 4 2 4712 temp 21 3 4711 resist 99 3 4711 thick 4 3 4711 temp 19 4 4712 resist 98 4 4712 thick 7 4 4712 temp 22 Thanks a lot for your help. With best regards Thorsten
try the following
df2 <- reshape(df1[-1], idvar = "id", direction = "long",
timevar = "Param", times = c("resist",
"thick", "temp"),
varying = list(c("resist", "thick",
"temp")))
df2 <- df2[order(df2$id, df2$desc), ]
rownames(df2) <- 1:nrow(df2)
df2
I hope it helps.
Best,
Dimitris
----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven
Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
http://www.student.kuleuven.be/~m0390867/dimitris.htm
----- Original Message -----
From: "Thorsten Muehge" <MUEHGE at de.ibm.com>
To: <R-help at stat.math.ethz.ch>
Sent: Tuesday, November 07, 2006 10:10 AM
Subject: [R] Reformat a data frame
>
> Hello Experts,
> how do I reformat a data frame in the way described below:
>
>
> df1:
> ID desc resist thick temp
> 1 4711 100 5 20
> 2 4712 101 4 21
> 3 4711 99 3 19
> 4 4712 98 7 22
>
> TO
>
> df2:
> id desc Param Value
> 1 4711 resist 100
> 1 4711 Thick 5
> 1 4711 temp 20
> 2 4712 resist 101
> 2 4712 Thick 4
> 2 4712 temp 21
> 3 4711 resist 99
> 3 4711 thick 4
> 3 4711 temp 19
> 4 4712 resist 98
> 4 4712 thick 7
> 4 4712 temp 22
>
> Thanks a lot for your help.
> With best regards
> Thorsten
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>
Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
In addition to the reshape solution posted one could use melt from the reshape package: melt(df1, id = 1:2) On 11/7/06, Thorsten Muehge <MUEHGE at de.ibm.com> wrote:> > Hello Experts, > how do I reformat a data frame in the way described below: > > > df1: > ID desc resist thick temp > 1 4711 100 5 20 > 2 4712 101 4 21 > 3 4711 99 3 19 > 4 4712 98 7 22 > > TO > > df2: > id desc Param Value > 1 4711 resist 100 > 1 4711 Thick 5 > 1 4711 temp 20 > 2 4712 resist 101 > 2 4712 Thick 4 > 2 4712 temp 21 > 3 4711 resist 99 > 3 4711 thick 4 > 3 4711 temp 19 > 4 4712 resist 98 > 4 4712 thick 7 > 4 4712 temp 22 > > Thanks a lot for your help. > With best regards > Thorsten > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >
You want to "reshape" from "wide" into "long"
format: see "help(reshape)".
For your example this should look something like
df2 <- reshape(df1,
varying=list(c("resist","thick","temp")),
direction="long",
v.names=c("Value"), timevar="Param",
idvar="ID",
times=c("resist","thick","temp"))
will give you roughly what you want - maybe not in the exact order as listed
below for df2, but you can "sort' then.
You are "merging" all value for three variables in the wide format,
into one variable ("Value") in the "long" format...
Try it, I did not test the above, but have done similar things before along
those lines.
Michael Jerosch-Herold
>>> "Thorsten Muehge" <MUEHGE@de.ibm.com> 11/07/06 1:10
AM >>>
Hello Experts,
how do I reformat a data frame in the way described below:
df1:
ID desc resist thick temp
1 4711 100 5 20
2 4712 101 4 21
3 4711 99 3 19
4 4712 98 7 22
TO
df2:
id desc Param Value
1 4711 resist 100
1 4711 Thick 5
1 4711 temp 20
2 4712 resist 101
2 4712 Thick 4
2 4712 temp 21
3 4711 resist 99
3 4711 thick 4
3 4711 temp 19
4 4712 resist 98
4 4712 thick 7
4 4712 temp 22
Thanks a lot for your help.
With best regards
Thorsten
[[alternative HTML version deleted]]