dplyr 0.7的发布包括对dplyr 进行编程的重大改进.我仔细阅读了本文档,并试图了解它对我使用dplyr的影响.
这是我在使用dplyr构建报告和聚合函数时使用的常用习惯用法:
my_report <- function(data, grouping_vars) {
data %>%
group_by_(.dots=grouping_vars) %>%
summarize(x_mean=mean(x), x_median=median(x), ...)
}
Run Code Online (Sandbox Code Playgroud)
这grouping_vars
是一个字符串向量.
我喜欢这个成语,因为我可以从其他地方传递字符串向量,例如文件或Shiny应用程序的反应性UI,但它对于交互式工作也不是太糟糕.
但是,在使用dplyr vignette的新编程中,我没有看到使用新的dplyr可以完成这样的事情的示例.我只看到传递字符串不再是正确方法的示例,我必须使用quosures.
我很高兴采用quosures,但是我如何从字符串到dplyr预期的这些情况呢?期望整个R生态系统向dplyr提供数据似乎是不可行的 - 很多时候我们将获得字符串并且它们必须被转换.
这是一个示例,显示您现在应该做什么,以及我的旧习语如何不起作用:
library(dplyr)
grouping_vars <- quo(am)
mtcars %>%
group_by(!!grouping_vars) %>%
summarise(mean_cyl=mean(cyl))
#> # A tibble: 2 × 2
#> am mean_cyl
#> <dbl> <dbl>
#> 1 0 6.947368
#> 2 1 5.076923
grouping_vars <- "am"
mtcars %>%
group_by(!!grouping_vars) %>%
summarise(mean_cyl=mean(cyl))
#> # A tibble: 1 × 2
#> `"am"` mean_cyl
#> <chr> <dbl>
#> 1 am 6.1875
Run Code Online (Sandbox Code Playgroud)
mt1*_*022 12
dplyr
将有一个专门的group_by函数group_by_at
来处理多个分组变量.使用该_at
家庭的新成员会容易得多:
# using the pre-release 0.6.0
cols <- c("am","gear")
mtcars %>%
group_by_at(.vars = cols) %>%
summarise(mean_cyl=mean(cyl))
# Source: local data frame [4 x 3]
# Groups: am [?]
#
# am gear mean_cyl
# <dbl> <dbl> <dbl>
# 1 0 3 7.466667
# 2 0 4 5.000000
# 3 1 4 4.500000
# 4 1 5 6.000000
Run Code Online (Sandbox Code Playgroud)
该.vars
参数接受由vars
以下内容生成的字符/数字向量或列名:
.vars
由vars()生成的列列表,或列名称的字符向量,或列位置的数字向量.
ale*_*yes 11
这是我为自己写的快速而肮脏的参考资料.
# install.packages("rlang")
library(tidyverse)
dat <- data.frame(cat = sample(LETTERS[1:2], 50, replace = TRUE),
cat2 = sample(LETTERS[3:4], 50, replace = TRUE),
value = rnorm(50))
Run Code Online (Sandbox Code Playgroud)
使用rlang::sym
和将字符串转换为符号对象rlang::syms
.
summ_var <- "value"
group_vars <- c("cat", "cat2")
summ_sym <- rlang::sym(summ_var) # capture a single symbol
group_syms <- rlang::syms(group_vars) # creates list of symbols
dat %>%
group_by(!!!group_syms) %>% # splice list of symbols into a function call
summarize(summ = sum(!!summ_sym)) # slice single symbol into call
Run Code Online (Sandbox Code Playgroud)
如果您使用!!
或!!!
不使用dplyr
函数,您将收到错误.
内部功能的使用rlang::sym
和rlang::syms
内部功能相同.
summarize_by <- function(df, summ_var, group_vars) {
summ_sym <- rlang::sym(summ_var)
group_syms <- rlang::syms(group_vars)
df %>%
group_by(!!!group_syms) %>%
summarize(summ = sum(!!summ_sym))
}
Run Code Online (Sandbox Code Playgroud)
然后我们可以summarize_by
用字符串参数调用.
summarize_by(dat, "value", c("cat", "cat2"))
Run Code Online (Sandbox Code Playgroud)
summ_quo <- quo(value) # capture a single variable for NSE
group_quos <- quos(cat, cat2) # capture list of variables for NSE
dat %>%
group_by(!!!group_quos) %>% # use !!! with both quos and rlang::syms
summarize(summ = sum(!!summ_quo)) # use !! both quo and rlang::sym
Run Code Online (Sandbox Code Playgroud)
enquo
而不是quo
.quos
没关系!?summarize_by <- function(df, summ_var, ...) {
summ_quo <- enquo(summ_var) # can only capture a single value!
group_quos <- quos(...) # captures multiple values, also inside functions!?
df %>%
group_by(!!!group_quos) %>%
summarize(summ = sum(!!summ_quo))
}
Run Code Online (Sandbox Code Playgroud)
然后我们的函数调用是
summarize_by(dat, value, cat, cat2)
Run Code Online (Sandbox Code Playgroud)
如果要按可能多个列进行分组,则可以使用 quos
grouping_vars <- quos(am, gear)
mtcars %>%
group_by(!!!grouping_vars) %>%
summarise(mean_cyl=mean(cyl))
# am gear mean_cyl
# <dbl> <dbl> <dbl>
# 1 0 3 7.466667
# 2 0 4 5.000000
# 3 1 4 4.500000
# 4 1 5 6.000000
Run Code Online (Sandbox Code Playgroud)
现在,似乎没有一种很好的方法可以将字符串变成混乱.这是一种可行的方法
cols <- c("am","gear")
grouping_vars <- rlang::parse_quosures(paste(cols, collapse=";"))
mtcars %>%
group_by(!!!grouping_vars) %>%
summarise(mean_cyl=mean(cyl))
# am gear mean_cyl
# <dbl> <dbl> <dbl>
# 1 0 3 7.466667
# 2 0 4 5.000000
# 3 1 4 4.500000
# 4 1 5 6.000000
Run Code Online (Sandbox Code Playgroud)