在dplyr group_by中选择减运算符

mmu*_*urr 5 r dplyr

有没有人知道在使用时选择"all-but-one"(或"all-but-a-few")列的快速方法dplyr::group_by?最后,我只想在删除一些选择列后聚合所有不同的行,但我不想每次都明确列出所有分组列(因为在我的分析中这些列经常被添加和删除).

例:

 > df <- data_frame(a = c(1,1,2,2), b = c("foo", "foo", "bar", "bar"), c = runif(4))
 > df
 Source: local data frame [4 x 3]

       a     b          c
   (dbl) (chr)      (dbl)
 1     1   foo 0.95460749
 2     1   foo 0.05094088
 3     2   bar 0.93032589
 4     2   bar 0.40081121
Run Code Online (Sandbox Code Playgroud)

现在我想通过a和聚合b,所以我可以这样做:

 > df %>% group_by(a, b) %>% summarize(mean(c))
 Source: local data frame [2 x 3]
 Groups: a [?]

       a     b   mean(c)
   (dbl) (chr)     (dbl)
 1     1   foo 0.5027742
 2     2   bar 0.6655686
Run Code Online (Sandbox Code Playgroud)

大.但是,我真的希望能够做一些事情,比如指定not c,类似于dplyr::select(-c):

 > df %>% select(-c)
 Source: local data frame [4 x 2]

       a     b
   (dbl) (chr)
 1     1   foo
 2     1   foo
 3     2   bar
 4     2   bar
Run Code Online (Sandbox Code Playgroud)

但是group_by可以应用表达式,因此等价物不起作用:

 > df %>% group_by(-c) %>% summarize(mean(c))
 Source: local data frame [4 x 2]

            -c    mean(c)
         (dbl)      (dbl)
 1 -0.95460749 0.95460749
 2 -0.93032589 0.93032589
 3 -0.40081121 0.40081121
 4 -0.05094088 0.05094088
Run Code Online (Sandbox Code Playgroud)

任何人都知道我是否只是缺少基本功能或快捷方式来帮助我快速完成这项工作?

例如使用情况:如果df突然获得了一个新的列d,我想下游代码到现在聚集了独特的组合a,b, d,没有我不得不明确添加dgroup_by通话).

use*_*691 1

在当前版本的 dplyr 中,函数group_by_at与 一起vars实现了此目标:

df %>% group_by_at(vars(-c)) %>% summarize(mean(c))
# A tibble: 2 x 3
# Groups:   a [?]
      a     b  `sum(c)`
  <dbl> <chr>     <dbl>
1     1   foo 0.9851376
2     2   bar 1.0954412
Run Code Online (Sandbox Code Playgroud)

似乎已于 2017 年 6 月在 dplyr 0.7.0 中引入