wit*_*tek 5 r dplyr summarize rlang quosure
使用dplyr启动版本0.7时,不推荐使用以下划线结尾的方法(例如summarize_group_by_),因为我们应该使用quosures.
请参阅:https: //cran.r-project.org/web/packages/dplyr/vignettes/programming.html
我试图使用quo和!!实现以下示例
工作范例:
df <- data.frame(x = c("a","a","a","b","b","b"), y=c(1,1,2,2,3,3), z = 1:6)
lFG <- df %>%
group_by( x,y)
lFG %>% summarize( min(z))
Run Code Online (Sandbox Code Playgroud)
但是,在这种情况下,我需要实现要分组的列,并将汇总指定为字符串.
cols2group <- c("x","y")
col2summarize <- "z"
Run Code Online (Sandbox Code Playgroud)
我怎样才能得到与上述相同的例子呢?
Ron*_*hah 12
从dplyr1.0.0 开始您可以使用across:
library(dplyr)
cols2group <- c("x","y")
col2summarize <- "z"
df %>%
group_by(across(all_of(cols2group))) %>%
summarise(across(all_of(col2summarize), min)) %>%
ungroup
# x y z
# <chr> <dbl> <int>
#1 a 1 1
#2 a 2 3
#3 b 2 4
#4 b 3 5
Run Code Online (Sandbox Code Playgroud)
为此,您现在可以使用_at动词的版本
df %>%
group_by_at(cols2group) %>%
summarize_at(.vars = col2summarize, .funs = min)
Run Code Online (Sandbox Code Playgroud)