[R] Reshape a data set

Henrique Dallazuanna wwwhsd at gmail.com
Wed Dec 16 11:57:31 CET 2009


Try this also:

reshape(test, direction = 'wide', idvar = 'subject', timevar = 'coder')

On Sun, Dec 13, 2009 at 6:04 PM, dadrivr <dadrivr at gmail.com> wrote:
>
> I am trying to reshape a data set.  Could someone please help me with the
> reshape, cast, and melt functions?  I am new to R and I have tried reading
> up on how to use the reshape package, but I am very confused.  Here is an
> example of what I am trying to do:
>     subject coder score time
> [1,]       1     1    20    5
> [2,]       1     2    30    4
> [3,]       2     3    10   10
> [4,]       2     2     5   12
> [5,]       3     2    15   NA
> [6,]       3     1    NA   13
>
> Reshape to:
>     subject coder.1  score.1 time.1 coder.2 score.2 time.2
> [1,]  1           1         20       5        2          30      4
> [2,]  2           3         10      10       2           5      12
> [3,]  3           2         15      NA       1          NA     13
>
> Here is the code to setup the original data set that I need to reshape:
> subject <- c(1,1,2,2,3,3)
> coder <- c(1,2,3,2,2,1)
> score <- c(20,30,10,5,15,NA)
> time <- c(5,4,10,12,NA,13)
> mydata <- cbind(subject,coder,score,time)
>
> Here is where I'm not sure:
> library(reshape)
> mydata_melt <- melt(mydata, id="subject", na.rm=TRUE)
> mydata_cast <- cast(mydata, ???
>
> Any help would be greatly appreciated.  Thanks so much!
> -Isaac
> --
> View this message in context: http://n4.nabble.com/Reshape-a-data-set-tp963104p963104.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>



-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O




More information about the R-help mailing list