[R] Help with reshaping data.frame

hadley wickham h.wickham at gmail.com
Mon Aug 3 23:57:26 CEST 2009


On Mon, Aug 3, 2009 at 5:23 PM, <davidr at rhotrading.com> wrote:
> I'm having trouble reshaping a data.frame from long to wide.
> (I think that's the right terminology; feel free to educate me.)
> I've looked at the reshape function and package and plyr package,
> but I can't quite figure out how to do this after a dozen variations.
>
> I have a data.frame with more levels than this, but similar to:
>
>> tst
>   K1 K2 K3   V1 V2  V3
> 1  10  D  a 0.08 99 105
> 2  20  D  a 0.00 79 522
> 3  30  D  a 0.31 70 989
> 5  20  E  a 0.08 73 251
> 6  30  E  a   NA 38 323
> 7  10  D  b   NA 76 775
> 8  20  D  b 0.26 84 372
> 9  30  D  b 0.24 51 680
> 10 10  E  b 0.11 85 532
> 12 30  E  b 0.07 20 364
>> dput(tst)
> structure(list(K1 = c(10, 20, 30, 20, 30, 10, 20, 30, 10, 30),
>    K2 = structure(c(1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 2L), .Label =
> c("D",
>    "E"), class = "factor"), K3 = structure(c(1L, 1L, 1L, 1L,
>    1L, 2L, 2L, 2L, 2L, 2L), .Label = c("a", "b"), class = "factor"),
>    V1 = c(0.08, 0, 0.31, 0.08, NA, NA, 0.26, 0.24, 0.11, 0.07
>    ), V2 = c(99, 79, 70, 73, 38, 76, 84, 51, 85, 20), V3 = c(105,
>    522, 989, 251, 323, 775, 372, 680, 532, 364)), .Names = c("K1",
> "K2", "K3", "V1", "V2", "V3"), row.names = c(1L, 2L, 3L, 5L,
> 6L, 7L, 8L, 9L, 10L, 12L), class = "data.frame")
>> str(tst)
> 'data.frame':   10 obs. of  6 variables:
>  $ K1: num  10 20 30 20 30 10 20 30 10 30
>  $ K2: Factor w/ 2 levels "D","E": 1 1 1 2 2 1 1 1 2 2
>  $ K3: Factor w/ 2 levels "a","b": 1 1 1 1 1 2 2 2 2 2
>  $ V1: num  0.08 0 0.31 0.08 NA NA 0.26 0.24 0.11 0.07
>  $ V2: num  99 79 70 73 38 76 84 51 85 20
>  $ V3: num  105 522 989 251 323 775 372 680 532 364
>
> I want a data.frame with columns K1, D.a.V1, D.a.V2, D.a.V3, D.b.V1,
> D.b.V2, ..., E.b.V3,
> with NA's where there is missing data.
>
> Any direction would be appreciated. sessionInfo() below.

library(reshape)
tst_m <- melt(tst, id = c("K1","K2", "K3"))
cast(tst_m, K1 ~ K2 + K3 + variable)

Hadley

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




More information about the R-help mailing list