[R] Reshape a data set
xavier.chardon at free.fr
xavier.chardon at free.fr
Wed Dec 16 09:44:48 CET 2009
Hi,
You need an additional id column in your data frame, which represents the replication number within each subject (the "1", "2" in the column headings after reshaping in what you showed).
test <- data.frame(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))
test <- ddply(test, .(subject), transform, replication=seq_along(subject))
test_m <- melt(test, id=c("subject", "replication"))
cast(test_m, subject~variable+replication)
HTH,
Xavier
----- Mail Original -----
De: "dadrivr" <dadrivr at gmail.com>
À: r-help at r-project.org
Envoyé: Dimanche 13 Décembre 2009 21h04:17 GMT +01:00 Amsterdam / Berlin / Berne / Rome / Stockholm / Vienne
Objet: [R] Reshape a data set
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.
More information about the R-help
mailing list