R: new_quosures(NextMethod()) 中的错误:找不到函数 "new_quosures"

R: Error in new_quosures(NextMethod()) : could not find function "new_quosures"

考虑一个数据框:

data = data.frame(a=c(1,1,1,2,2,3),
              b=c("apples", "oranges", "apples", "apples", "apples", "grapefruit"),
              c=c(12, 22, 22, 45, 67, 28), 
              d=c("Monday", "Monday", "Monday", "Tuesday", "Wednesday", "Tuesday"),
              out = c(12, 14, 16, 18, 20, 22),
              rate = c(0.01, 0.02, 0.03, 0.04, 0.07, 0.06))

我正在尝试 group_by 并进行总结,但是我一直收到错误

Error in new_quosures(NextMethod()) : 
  could not find function "new_quosures"

我使用的代码如下:

model.data.dim.names <-  c("a", "b", "c")

data2 <- data %>% group_by_(.dots = model.data.dim.names) %>% summarise(
    mean_adj1 = (mean(out, na.rm=FALSE)),
    mean_adj2 = (mean(out)/mean(rate))
  )

请注意,这是虚拟数据,错误在 windows OS 中用虚拟数据重现。此外,我正在研究 Windows OS。此外,我尝试了以下方法:

  1. 删除 plyr
  2. 检查并编辑 NA/无限值
  3. 将数据框转换为数据 table 和 运行 代码

能否请您帮助我了解错误的根本原因或我可以使用的替代方法?

Answer: 

1) tidyr library screws up with it. Removing tidyr helps
2) use most updated dplyr library and group_by/ group_by_at/group_by(!!!syms(model.data.dim.names) works

group-by_ 函数已被弃用,目前最流行的方法是将字符向量转换为符号,然后取消引用将它们拼接成 group_by:

library(dplyr)

data %>%
  group_by(!!!syms(model.data.dim.names)) %>% 
  summarise(
    mean_adj1 = mean(out, na.rm=FALSE),
    mean_adj2 = mean(out) / mean(rate)
  )
## A tibble: 6 x 5
## Groups:   a, b [4]
#      a b              c mean_adj1 mean_adj2
#  <dbl> <fct>      <dbl>     <dbl>     <dbl>
#1     1 apples        12        12     1200 
#2     1 apples        22        16      533.
#3     1 oranges       22        14      700 
#4     2 apples        45        18      450 
#5     2 apples        67        20      286.
#6     3 grapefruit    28        22      367.

我们可以使用 dplyr 中的 group_by_at,它可以将字符串作为输入

library(dplyr)
data %>% 
   group_by_at(model.data.dim.names) %>% 
   summarise(
    mean_adj1 = mean(out, na.rm=FALSE),
    mean_adj2 = mean(out) / mean(rate)
  )
# A tibble: 6 x 5
# Groups:   a, b [4]
#      a b              c mean_adj1 mean_adj2
#  <dbl> <fct>      <dbl>     <dbl>     <dbl>
#1     1 apples        12        12     1200 
#2     1 apples        22        16      533.
#3     1 oranges       22        14      700 
#4     2 apples        45        18      450 
#5     2 apples        67        20      286.
#6     3 grapefruit    28        22      367.

我在使用 2 周前运行良好的代码时遇到了同样的错误。应用 dplyr::group_by() 时发生。我有 dplyr 包版本 0.7.6 并将其更新为 0.8.0.1。这已经解决了问题。