考虑具有以下结构的data.table:
DT = data.table(x=rep(c("b","a","c"),each=3), y=1:9)
DT
# x y
# 1: b 1
# 2: b 2
# 3: b 3
# 4: a 4
# 5: a 5
# 6: a 6
# 7: c 7
# 8: c 8
# 9: c 9
我希望在按y
进行分组时按照x
列的总和进行减少,即我希望:
# x y
# 1: c 7
# 2: c 8
# 3: c 9
# 4: a 4
# 5: a 5
# 6: a 6
# 7: b 1
# 8: b 2
# 9: b 3
我找到的唯一方法是在分组时使用'intragroup sum'创建一个新列,然后使用该列进行排序:
DT[, s:=sum(y), by=x][order(s,decreasing=TRUE), .(x,y)]
# x y
# 1: c 7
# 2: c 8
# 3: c 9
# 4: a 4
# 5: a 5
# 6: a 6
# 7: b 1
# 8: b 2
# 9: b 3
但我想这必须是更好的方法。有什么想法吗?