[R] Could anyone help me reshape this "wide" data into"longitudinal" one? Thanks

Robert W. Baer, Ph.D. rbaer at atsu.edu
Mon Nov 29 02:19:11 CET 2004


This might get you started:
# make data in original format
a=matrix(1:48,12,4)
colnames(a)<-c('var1','var2','var3','var4')
rownames(a)<-c('a1','a2','a3','b1','b2','b3','c1','c2','c3','d1','d2','d3')
a

#rehape the data
a=data.frame(a)
reshape(a[1:4],idvar="row",
timevar="column",varying=list(names(a[1:4])),direction="long")

Rob

----- Original Message ----- 
From: "Frank Duan" <fhduan at gmail.com>
To: <r-help at stat.math.ethz.ch>
Sent: Sunday, November 28, 2004 5:13 PM
Subject: [R] Could anyone help me reshape this "wide" data
into"longitudinal" one? Thanks


> Dear R people,
>
> I have a matrix like this:
>
>       var1   var2    var3    var4
> a1   7.1   7.2       8.1    8.2
> a2  10.5  10.6    ...       ...
> a3
> b1
> b2
> b3
> b4
> c1
> c2
> ...
>
> The matrix row names are "a1", "a2", ......  and the matrix column
> names are "var1", "var2", "var3" and "var4". Now I want to reshape
> this data into a longitudinal-formatted one like this:
>
>
> subject    seq   time    resp
> a              1      1        7.1
> a              1      2        7.2
> a              1      3        8.1
> a              1      4        8.2
> a              2      1       10.5
> a              2      2       10.6
> a              2      3         ...
> a              2      4         ...
> a              3      1         ...
> a              3      2
> a              3      3
> a              3      4
> b              1      1
> b              1      2
> b              1      3
> b              1      4
> b              2      1
> b              2      2
> b              2      3
> b              2      4
> ...             ...    ...
> ...             ...    ...
> ...             ...    ...
>
>
> I always met errors when using "reshape" function. Could anyone give
> me an idea how to do it?
>
> Many thanks to you,
>
> Frank
>
> ______________________________________________
> 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
>




More information about the R-help mailing list