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.
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.
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