英文:
Tidy eval for `by` in `dplyr::_join
问题
我正在编写一个函数,使用dplyr::_join
来连接两个数据集,其中by
参数是无引号传递的。我看到有不少解决这个问题的方法,但似乎都已经过时和/或不建议使用:
如何使用新的tidy eval方法来完成这个任务,可以在 这里 找到,该文强调了使用{{}}
的方法。看起来自这些旧答案以来,tidy eval已经更新,这就是我再次提问的原因 - 请让我知道是否应该简单地使用这些旧答案中的一个。
这里有一个示例来演示我的问题:
data("iris")
iris2 <- iris %>%
select(Species) %>%
filter(Species != "versicolor") %>%
mutate(id = case_when(Species == "virginica" ~ 1, TRUE ~ 2)) %>%
distinct()
### 这是我期望的输出
joined_iris <- iris %>%
inner_join(iris2, by = c("Species" = "Species"))
### 这是一个示例函数,我尝试在其中使用tidy eval
# -- 它不起作用
join_iris <- function(data1, data2, join_col1, join_col2) {
data_out <- data1 %>%
inner_join(
data2,
by = c({{ join_col1 }} = {{ join_col2 }})
)
data_out
}
join_iris(iris, iris2, Species, Species)
英文:
I am writing a function to join two datasets using dplyr::_join
where the by
terms are parameters passed in without quotes. I have seen quite a few solutions to this issue, but all seem to be dated and/or deprecated:
-
Use
rlang::quo_text
orpurrr::map_chr
in this answer and here which is superceded in the docs here -
Use
dplyr::ensym
in this answer which is listed as "no longer for normal usage" in the docs here
How would I accomplish this using the new tidy eval methods as found here which emphasizes the use of {{}}
? It seems like tidy eval has been updated since these older answers were given, which is why I am asking again -- please let me know if I should simply stick with one of these old answers.
Here is a toy example to demonstrate my question:
data("iris")
iris2 <- iris %>%
select(Species) %>%
filter(Species != "versicolor") %>%
mutate(id = case_when(Species == "virginica" ~ 1, T ~ 2)) %>%
distinct()
### This is my desired output
joined_iris <- iris %>% inner_join(iris2, by = ("Species" = "Species"))
### This is an example function where I attempt to use tidy eval
# -- it does not work
join_iris <- function(data1, data2, join_col1, join_col2) {
data_out <- data1 %>%
inner_join(
data2,
by = c({{ join_col1 }} = {{ join_col2 }})
)
data_out
}
join_iris(iris, iris2, Species, Species)
答案1
得分: 2
使用dplyr 1.1.0中的join_by()
:
join_iris <- function(data1, data2, col1, col2) {
inner_join(
data1,
data2,
by = join_by({{ col1 }} == {{ col2 }})
)
}
英文:
Using join_by()
from dplyr 1.1.0:
join_iris <- function(data1, data2, col1, col2) {
inner_join(
data1,
data2,
by = join_by({{ col1 }} == {{ col2 }})
)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论