我的数据框看起来像这样:
Game Team Value
1 A 0.6
1 B 0.5
2 C 1.2
2 D 0.3
我想创建一个新列来计算'值的差异'对于给定的游戏,所以它将是:
difference
0.1
-0.1
0.9
-0.9
换句话说,我想通过“游戏”进行分组。但我不太清楚如何做到这一点
答案 0 :(得分:6)
使用ave
的简单基础R解决方案可能是
with(df, ave(Value, Game, FUN = diff)) * c(-1, 1)
# [1] 0.1 -0.1 0.9 -0.9
答案 1 :(得分:3)
如果每组只有两个元素:
library(data.table)
# using @DavidArenburg clean trick
# otherwise c(diff(rev(Value)),diff(Value)) makes it
setDT(df)[,difference:=diff(Value)*c(-1,1), by = Game][]
Game Team Value difference
1: 1 A 0.6 0.1
2: 1 B 0.5 -0.1
3: 2 C 1.2 0.9
4: 2 D 0.3 -0.9
答案 2 :(得分:1)
您还可以使用aggregate
c(t(aggregate(Value~Game, df, function(x) c(diff(rev(x)), diff(x)))[, -1]))
[1] 0.1 -0.1 0.9 -0.9
答案 3 :(得分:1)
使用dplyr
library(dplyr)
df1 %>%
group_by(Game) %>%
mutate(difference = diff(Value)*c(-1,1))
# Game Team Value difference
# (int) (chr) (dbl) (dbl)
#1 1 A 0.6 0.1
#2 1 B 0.5 -0.1
#3 2 C 1.2 0.9
#4 2 D 0.3 -0.9