Change the data from long to half-width in R

I have data in which each participant made 3 judgments for each of 9 objects (27 judgments). 9 objects changed in the 3x3 design (inside the objects), so there are 2 factors.

I start with ID + 27 data columns and I need to have

  • ID
  • 2 factor columns: performance, situation
  • 3 columns of value: Success, ProbAdmit, Admit

I read the manuals on reseape () and melt () and cast (), but still could not figure out what I need to do to make this happen. Here is my current progress from which you can see my actual data.

scsc3 <- read.csv("http://swift.cbdr.cmu.edu/data/SCSC3-2006-10-10.csv")
library(reshape)
scsc3.long <- melt(scsc3,id="Participant")
scsc3.long <- cbind(scsc3.long,colsplit(scsc3.long$variable,split="[.]",names=c("Item","Candidate","Performance","Situation")))
scsc3.long$variable <- NULL
scsc3.long$Candidate <- NULL

The above code leaves me with the following:

Participant  value  Item      Performance  Situation
4001         5.0    Success   GL           IL
4001         60     ProbAdmit GL           IL
4001         1      Admit     GL           IL
4002         ....

What I need is a dataframe

Participant Performance  Situation SuccessValue ProbAdmitValue AdmitValue
4001        GL           IL        5.0          60             1
...

Thank!

+3
source share
1 answer

Try the following:

require(reshape2)
> dcast(scsc3.long, 
        Participant + Performance + Situation ~ Item, 
        value_var = 'value' )

  Participant Performance Situation Admit ProbAdmit Success
1        4001          GH        IH     1       100       7
2        4001          GH        IL     1        50       5
3        4001          GH        IM     1        60       5
4        4001          GL        IH     0        40       3
5        4001          GL        IL     0         0       2
6        4001          GL        IM     0        40       4
...

, dcast: "" Participant + Performance + Situation - Item, .. Admit, ProbAdmit, Success. value_var = 'value' , value "-".

+9

All Articles