[R] reshape
Thomas Lumley
tlumley at u.washington.edu
Mon Sep 9 20:26:38 CEST 2002
On Mon, 9 Sep 2002, Ngayee J Law wrote:
> Hi,
>
> I have a problem using the function 'reshape' for the following dataset:
>
> input:
> X Y Z T.U.1 T.U.2 T.V.1 T.V.2
> 1.00 1.00 A 24.94 24.74 34.65 35.33
> 1.00 3.00 A 26.03 25.87 25.76 26.12
> 1.00 1.00 B 24.76 24.63 34.34 34.48
> 1.00 3.00 B 26.15 25.98 25.96 25.66
> 2.00 1.00 A 24.40 24.26 34.55 36.53
> 2.00 4.00 A 24.55 24.40 35.52 39.61
> 2.00 1.00 B 25.00 24.76 35.32 36.67
> 2.00 4.00 B 25.96 25.70 26.00 25.62
>
> target output:
> X Y Z T Time W
> 1.00 1.00 A U 1 24.94
> 1.00 1.00 A U 2 24.74
> 1.00 1.00 A V 1 34.65
> 1.00 1.00 A V 2 35.33
> 1.00 3.00 A U 1 26.03
> 1.00 3.00 A U 2 25.87
> 1.00 3.00 A V 1 25.76
> 1.00 3.00 A V 2 26.12
> 1.00 1.00 B U 1 24.76
> 1.00 1.00 B U 2 24.63
> 1.00 1.00 B V 1 34.34
> 1.00 1.00 B V 2 34.48
> 1.00 3.00 B U 1 26.15
> 1.00 3.00 B U 2 25.98
> 1.00 3.00 B V 1 25.96
> 1.00 3.00 B V 2 25.66
> 2.00 1.00 A U 1 24.40
> 2.00 1.00 A U 2 24.26
> 2.00 1.00 A V 1 34.55
> 2.00 1.00 A V 2 36.53
> 2.00 4.00 A U 1 24.55
> 2.00 4.00 A U 2 24.40
> 2.00 4.00 A V 1 35.52
> 2.00 4.00 A V 2 39.61
> 2.00 1.00 B U 1 25.00
> 2.00 1.00 B U 2 24.76
> 2.00 1.00 B V 1 35.32
> 2.00 1.00 B V 2 36.67
> 2.00 4.00 B U 1 25.96
> 2.00 4.00 B U 2 25.70
> 2.00 4.00 B V 1 26.00
> 2.00 4.00 B V 2 25.62
>
>
> There are more than 1 varying for the idvar (X, Y, Z) and there are two
> variables
> for the varying option (U, V). Any ideas how to use reshape for this
> problem?
The simplest way is to do it in two steps
Suppose the data are in data frame `a'
b<-reshape(a,direction="long",
varying=list(c("T.U.1","T.U.2"), c("T.V.1","T.V.2")),
v.names=c("U","V"))
looks like
X Y Z time U V id
1.1 1 1 A 1 24.94 34.65 1
2.1 1 3 A 1 26.03 25.76 2
3.1 1 1 B 1 24.76 34.34 3
4.1 1 3 B 1 26.15 25.96 4
5.1 2 1 A 1 24.40 34.55 5
6.1 2 4 A 1 24.55 35.52 6
7.1 2 1 B 1 25.00 35.32 7
8.1 2 4 B 1 25.96 26.00 8
1.2 1 1 A 2 24.74 35.33 1
2.2 1 3 A 2 25.87 26.12 2
3.2 1 1 B 2 24.63 34.48 3
4.2 1 3 B 2 25.98 25.66 4
5.2 2 1 A 2 24.26 36.53 5
6.2 2 4 A 2 24.40 39.61 6
7.2 2 1 B 2 24.76 36.67 7
8.2 2 4 B 2 25.70 25.62 8
Now, you want to reshape again, but specify that the time variable is
called "T", with values "U" and "V". You also want to drop the id
variable.
reshape(b[,-7],direction="long",
varying=list(c("U","V")), v.names="W",
times=c("U","V"), timevar="T")
Again, you probably want to drop the `id' variable and if you care
about the order of rows, sort on X and Y.
-thomas
Thomas Lumley Asst. Professor, Biostatistics
tlumley at u.washington.edu University of Washington, Seattle
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list