我有以下数据
Exp =我的数据帧
dt<-data.table(Game=c(rep(1,9),rep(2,3)),
Round=rep(1:3,4),
Participant=rep(1:4,each=3),
Left_Choice=c(1,0,0,1,1,0,0,0,1,1,1,1),
Total_Points=c(5,15,12,16,83,7,4,8,23,6,9,14))
> dt
Game Round Participant Left_Choice Total_Points
1: 1 1 1 1 5
2: 1 2 1 0 15
3: 1 3 1 0 12
4: 1 1 2 1 16
5: 1 2 2 1 83
6: 1 3 2 0 7
7: 1 1 3 0 4
8: 1 2 3 0 8
9: 1 3 3 1 23
10: 2 1 4 1 6
11: 2 2 4 1 9
12: 2 3 4 1 14现在,我需要做以下几点:
因此,在第一阶段,我认为我应该计算以下几点:
Game Participant Percent_left Total_Points (in last round)
1 1 33% 12
1 2 66% 7
1 3 33% 23
2 4 100% 14最终结果应该是这样的:
Game Left_Choice Total_Poins (average)
1 >35% 17.5= (12+23)/2
1 <35%<70% 7
1 >70% NA
2 >35% NA
2 <35%<70% NA
2 >70% 14 请帮助!:)
发布于 2015-05-26 01:48:14
在data.table工作
1:具有by的简单群均值
dt[,pct_left:=mean(Left_Choice),by=.(Game,Participant)]2:使用cut;不完全清楚,但我认为您需要include.lowest=T。
dt[,pct_grp:=cut(pct_left,breaks=seq(0,1,by=.2),include.lowest=T)]3:稍微复杂一些的群体平均为by
dt[Round==max(Round),end_mean:=mean(Total_Points),by=.(pct_grp,Game)](如果您只想要精简的表,请改用.(end_mean=mean(Total_Points)))。
您没有明确说明是否存在全局最大轮数(即,是否所有游戏都以相同的轮数结束);这是在上面假设的。为了提供一个确切的替代方案,您必须更清楚地了解这一点,但我建议从逐轮定义它开始:
dt[,end_mean:=mean(Total_Points),by=.(pct_grp,Game,Round)]https://stackoverflow.com/questions/30446067
复制相似问题