(和朋友)中的by
参数dplyr::left_join
需要一个字符串列表。我的代码已经将感兴趣的变量定义为 quosures 列表,因此我想通过将 quosures 列表转换为字符串列表来避免再次写下该列表。
我四处rlang
寻找文档,但没有找到现有的解决方案。函数族可以将quo_expr
quosures拼接和转换为单个字符串,但它们不会产生字符串列表:
library(rlang)
# this works
sepal_length <- quo(Sepal.Length)
quo_text(sepal_length)
# this doesnt
sepaldims <- quos(Species, Sepal.Length)
quo_text(sepaldims)
这是一个有用的示例:
library(dplyr)
# group_by accepts a list of quosures
sepaldims <- quos(Species, Sepal.Length)
iris_sepal <- iris %>%
group_by(!!!sepaldims) %>%
summarize(sepal_width = mean(Sepal.Width), petal_width = mean(Petal.Width))
petaldims <- quos(!!!sepaldims, Petal.Length)
iris_petal <- iris %>%
group_by(!!!petaldims) %>%
summarize(sepal_width = mean(Sepal.Width), petal_width = mean(Petal.Width))
# but left join expects a list of strings
iris_merge <- left_join(iris_sepal, iris_petal, by = c("Species", "Sepal.Length"))