2011-06-13 73 views
3

我有一些數據,其中每個參與者對9個對象(27個判斷)中的每一個做出3個判斷。這9個物體在3x3設計(主體內)中有所不同,因此有2個因素。將數據從長整形到半寬整形R

我開始用ID + 27的數據列,我需要有

  • ID
  • 2係數列:性能,狀況
  • 3值列:成功,ProbAdmit,承認

我已經閱讀了關於reshape()和melt()和cast()的手冊,但還沒有弄清楚我需要做些什麼來實現它。這是我目前的進度,您可以從中看到我的實際數據。

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 

上面的代碼給我留下了這一點:

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

我需要的是這樣的

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

感謝一個數據幀!

回答

9

試試這個:想起了什麼dcast正在做的是

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

的一種方式:「中投」的數據幀成寬幅 其中的行是Participant + Performance + Situation組合和 列是Item的不同可能值,即Admit, ProbAdmit, Successvalue_var = 'value'表示應爲每個「行 - 列」組合顯示value列的條目。

+0

優秀!謝謝!我還沒有找到reshape2或dcast,但真的,這個公式是我真正需要的。現在你做到了,當然看起來很明顯。謝謝! – 2011-06-13 18:39:50

+0

@Sam歡迎您。 – 2011-06-13 19:11:38