[R] data.frame, converting row data to columns

hadley wickham h.wickham at gmail.com
Sat Apr 4 19:15:25 CEST 2009


On Sat, Apr 4, 2009 at 12:09 PM, ds <dhshanab at acad.umass.edu> wrote:
>
> I have a data frame something like:
>                      name         wrist
> nLevel            emot
> 1                    4094          3.34                    1
> frustrated
> 2                    4094          3.94                    1
> frustrated
> 3                    4094            NA                    1
> frustrated
> 4                    4094          3.51                    1
> frustrated
> 5                    4094          3.81                    1
> frustrated
> 6                    4101          2.62                    4
> excited
> 7                    4094          2.65                    1
> frustrated
> 8                    4101            NA                    4
> excited
> 9                    4101          0.24                    4
> excited
> 10                   4101          0.23                    4
> excited
>
> I am trying to change it to this:
>
>              name          nLevel           emot          w1
> w2       w3      w4      w5     w5
>                4094                   1      frustrated    3.34
> 3.94      NA    3.51     3.81    2.65
>                4101                   4      excited
> 2.62       NA     0.24    0.23      NA    NA
>
> The nLevel and emot will never vary with the name, so there can be one
> row per name.  But I need the wrist measurements to be in the same
> row.  The number of wrist measures are variable, so I could fill in
> with NAs .  But I really just need help with reshaping the data frame
>
> I think I had some success with the melt
>
> x
> =
> melt
> .data
> .frame(bsub,id.vars=c("name","nLevel","emot"),measure.vars=c("wrist"))
>
> But I can't figure out the cast to get the wrist values in the rows.

cast(x, ... ~ nLevel) ?

If that doesn't work, please provide a minimal reproducible example.

Hadley

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


More information about the R-help mailing list