是否可以否定data.table R的by参数中的列

时间:2017-05-12 14:21:05

标签: r data.table

我想在剩下的列中指定sum列和group。似乎没有办法否定by参数中的列,就像它可能的.SDcols一样。那是对的吗?我找到了另一种方法,但是想知道我是否缺少一些data.table魔法。

a=data.table(a=c(1,3,1), b=c(2,2,3), c=c(5,6,7))

not_gp = c('b','c')
# this works but is not what I want!
a[,lapply(.SD,sum),by=not_gp,.SDcols =!not_gp]


# what I want, but doesn't work
a[,lapply(.SD,sum),by=!not_gp,.SDcols =not_gp]
# Error in !not_gp : invalid argument type
#does work
gp = names(a)[!names(a) %in% not_gp]
a[,lapply(.SD,sum),by=gp,.SDcols =not_gp]
# also works
a[,lapply(.SD,sum),by=gp]

1 个答案:

答案 0 :(得分:1)

您可以使用:

a[, lapply(.SD, sum), by = setdiff(names(a), not_gp), .SDcols = not_gp]

这给了你:

   a b  c
1: 1 5 12
2: 3 2  6