1

考虑一个数据框:

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 操作系统中重现。此外,我正在使用 Windows 操作系统。此外,我尝试了以下方法:

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

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

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
4

3 回答 3

1

group-by_函数已弃用,当前的 tidyeval 方法是将字符向量转换为符号,然后将它们取消引用拼接成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.
于 2019-04-15T19:58:37.820 回答
1

我们可以使用group_by_atfrom dplyrwhich 可以将字符串作为输入

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.
于 2019-04-16T05:12:38.353 回答
1

我在 2 周前运行良好的代码中遇到了同样的错误。申请时就出现了dplyr::group_by()。我有 dplyr 软件包版本 0.7.6 并将其更新为 0.8.0.1。这解决了这个问题。

于 2019-04-24T08:09:12.677 回答