[R] reShape question

Jean-Louis Abitbol abitbol at sent.com
Mon Aug 22 12:55:09 CEST 2005


Dear R helpers,

The following output, from Hmisc reShape examples, illustrates how to
reshape from a data frame with repeated measures. 

I would need to do exactly the opposite ie get sbp1 to 3 and dbp1 to 3
from sbp and and dbp.

Is there a way to do that without subsetting,  renaming and binding ?

Many thanks for any help,

Regards, JL

> set.seed(33)
>      n <- 4
>      w <- data.frame(age=rnorm(n, 40, 10),
+                      sex=sample(c('female','male'), n,TRUE),
+                      sbp1=rnorm(n, 120, 15),
+                      sbp2=rnorm(n, 120, 15),
+                      sbp3=rnorm(n, 120, 15),
+                      dbp1=rnorm(n,  80, 15),
+                      dbp2=rnorm(n,  80, 15),
+                      dbp3=rnorm(n,  80, 15), row.names=letters[1:n])
>      options(digits=3)
>      w
   age    sex sbp1 sbp2 sbp3 dbp1 dbp2  dbp3
a 38.6 female  109  123  131 81.8 90.9  88.9
b 39.6 female  132  120  120 71.1 86.9 110.0
c 50.1   male  131  148  118 73.4 82.8  52.4
d 38.4 female  104  124  125 84.4 83.5  67.1
> reShape(w, base=c('sbp','dbp'), reps=3, timevar='week', times=c(0,3,12))
    week  age    sex sbp   dbp
a 1    0 38.6 female 109  81.8
a 2    3 38.6 female 123  90.9
a 3   12 38.6 female 131  88.9
b 1    0 39.6 female 132  71.1
b 2    3 39.6 female 120  86.9
b 3   12 39.6 female 120 110.0
c 1    0 50.1   male 131  73.4
c 2    3 50.1   male 148  82.8
c 3   12 50.1   male 118  52.4
d 1    0 38.4 female 104  84.4
d 2    3 38.4 female 124  83.5
d 3   12 38.4 female 125  67.1




More information about the R-help mailing list