[R] reshape with two time variables

hadley wickham h.wickham at gmail.com
Sat Jan 31 01:54:12 CET 2009


On Fri, Jan 30, 2009 at 5:57 PM, Neil Stewart
<neil.stewart at warwick.ac.uk> wrote:
> I have a data frame in wide format that I'd like to convert to long format.
> For example, in wide format I have:
>
>        id      A1B1    A1B2    A2B1    A2B2
> 1       1       400     475     420     510
> 2       2       390     500     470     472
> 3       3       428     512     555     610
> 4       4       703     787     801     822
> 5       5       611     634     721     705
> 6       6       543     522     612     788
> 7       7       411     488     506     623
> 8       8       654     644     711     795
>
> A is one repeated-measures variable with levels 1 and 2. B is a second
> repeated-measures variable also with levels 1 and 2. I'd like to end up
> with:
>
>        id      A       B       value
> 1       1       1       1       400
> 2       2       1       1       390
> 3       3       1       1       428
> 4       4       1       1       703
> 5       5       1       1       611
> 6       6       1       1       543
> 7       7       1       1       411
> 8       8       1       1       654
> 9       1       1       2       475
> 10      2       1       2       500
> 11      3       1       2       512
> 12      4       1       2       787
> 13      5       1       2       634
> 14      6       1       2       522
> 15      7       1       2       488
> 16      8       1       2       644
> 17      1       2       1       420
> 18      2       2       1       470
> 19      3       2       1       555
> 20      4       2       1       801
> 21      5       2       1       721
> 22      6       2       1       612
> 23      7       2       1       506
> 24      8       2       1       711
> 25      1       2       2       510
> 26      2       2       2       472
> 27      3       2       2       610
> 28      4       2       2       822
> 29      5       2       2       705
> 30      6       2       2       788
> 31      7       2       2       623
> 32      8       2       2       795
>
> I've been working with reshape and with the reshape package, but with no
> success for the two-time-variable case. Can anyone help please?

If you're using melt from the reshape package, once you've melted the
data, you'll need to do something like:

m$a <- as.numeric(substr(m$variable, 1, 1))
m$b <- as.numeric(substr(m$variable, 3, 3))
m$variable <- NULL

Hadley


-- 
http://had.co.nz/




More information about the R-help mailing list