在R中重新整理数据从长到半宽 [英] Reshape data from long to semi-wide in R

查看:124
本文介绍了在R中重新整理数据从长到半宽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有数据,每个参与者对9个对象中的每一个做出3个判断(27个判断)。 9个对象在3x3设计(主题内)中有所不同,所以有2个因素。



我从ID + 27数据列开始,我需要有




  • ID

  • 2个因素列:绩效,情况

  • 3个值列:Success,ProbAdmit,Admit



我已经阅读了关于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 )))
scsc3.long $ variable< - NULL
scsc3.long $ Candidate< - NULL

上面的代码让我看到:

 参与者价值项目绩效情况
4001 5.0成功GL IL
4001 60 ProbAdmit GL IL
4001 1承认GL IL
4002 ....

我需要的是像这样的数据框

 参与者绩效情况SuccessValue ProbAdmitValue AdmitValue 
4001 GL IL 5.0 60 1
...

谢谢!

解决方案

尝试这样:

  require(reshape2)
> dcast(scsc3.long,
参与者+表现+情况〜项目,
value_var ='value')

参与者表现情况承认ProbAdmit成功
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 正在做的是:将数据框投射为宽格式
,其中行是参与者+表现+情况的组合
列是不同的可能值项目,即承认,ProbAdmit,成功
value_var ='value'表示应显示列的条目行列组合。


I have data in which each participant made 3 judgments on each of 9 objects (27 judgments). The 9 objects varied in a 3x3 design (within subjects) so there are 2 factors.

I'm starting with ID + 27 data columns, and I need to have

  • ID
  • 2 factor columns: Performance, Situation
  • 3 value columns: Success, ProbAdmit, Admit

I have read the manuals on reshape() and melt() and cast() but haven't yet been able to figure out what I need to do to make it 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 this:

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 like this

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

Thanks!

解决方案

Try this:

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

One way to think of what dcast is doing is: "cast" the data-frame into a wide format where the rows are combinations of Participant + Performance + Situation and the columns are the different possible values of Item, i.e. Admit, ProbAdmit, Success. The value_var = 'value' indicates that the entries of the value column should be displayed, for each "Row-Column" combination.

这篇关于在R中重新整理数据从长到半宽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆