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

jim holtman jholtman at gmail.com
Sat Apr 4 19:28:19 CEST 2009


Does this do what you want:

> x <- read.table(textConnection("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"), header=TRUE)
> # add index
> x$indx <- ave(seq_along(x$emot), x$emot, FUN=function(z) seq(length(z)))
> require(reshape)
> y <- melt(x, measure='wrist')
> cast(y, name+nLevel+emot~indx)
  name nLevel       emot    1    2    3    4    5    6
1 4094      1 frustrated 3.34 3.94   NA 3.51 3.81 2.65
2 4101      4    excited 2.62   NA 0.24 0.23   NA   NA


On Sat, Apr 4, 2009 at 1: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.
>
>
> Thanks
>
> ds
>
>
> David H. Shanabrook
> dhshanab at acad.umass.edu
> 256-1019 (home)
>
>
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?




More information about the R-help mailing list