【发布时间】:2017-06-16 14:17:28
【问题描述】:
编辑:我把问题写成非结构化的方式,让我再试一次。
我想为下面的数据集创建两个新列,winner_total_points 和 loser_total_points。
winner <- c(1,2,3,4,1,2)
loser <- c(2,3,1,3,3,1)
winner_points <- c(5,4,12,2,1,6)
loser_points <- c(5,2,2,6,6,2)
test_data <- data.frame(winner, loser, winner_points, loser_points)
我想要这两列做的是winner_total_points 将获胜者(不包括本场比赛)作为获胜者和失败者获得的所有积分相加。
loser_total_points 的功能相同,但对于失败者而言。
请注意,winner 和 loser 列包含各自的玩家 ID。
现在,使用ave() 函数相当容易,但它仅适用于仅对列进行分组并为一列进行累积总和。
期望的输出:
winner loser winner_points loser_points winner_total loser_total
1 2 5 5 5 5
2 3 4 2 9 (5+4) 2
3 1 12 2 14 (2+12) 7 (5+2)
4 3 2 6 2 20 (2+12+6)
1 3 1 6 8 (5+2+1) 26 (2+12+6+6)
2 1 6 2 15 (5+4+6) 10 (5+2+1+2)
【问题讨论】:
-
请提供一个可重现的例子
-
我可以提供一个可以加载的示例数据集。
-
我不遵循逻辑。
-
@Sotos 我现在重写了这个问题。希望它现在更有意义。
-
我还是不清楚。