Hello,
I am using the join_by dplyr function many times using the same variables, say : by = join_by(x, y, z).
Is it possible to " pass " this character vector in a join_by to avoid repeating the variables.
vars <- c("x", "y", "z")
Thank you,
Fred.
You can do this with rlang
, actually I think quite easily since you're using tidyverse
functions. From the documentation on the rlang
injection operator !!
:
All data-masking verbs in the tidyverse support injection operators out of the box. With base functions, you need to use inject()
to enable !!
.
Maybe a workflow like this:
left <- tibble::tibble(
id_1 = c("A", "B", "C"),
id_2 = c("D", "D", "E"),
left_val = c(1, 2, 3)
)
right <- tibble::tibble(
id_1 = c("A", "B", "C"),
id_2 = c("D", "E", "E"),
right_val = c(4, 5, 6)
)
join_vars <- c("id_1", "id_2")
dplyr::left_join(
left,
right,
dplyr::join_by(!!!join_vars)
)
#> # A tibble: 3 × 4
#> id_1 id_2 left_val right_val
#> <chr> <chr> <dbl> <dbl>
#> 1 A D 1 4
#> 2 B D 2 NA
#> 3 C E 3 6
Created on 2024-09-27 with reprex v2.1.1.9000
2 Likes
Thank you, Eric for your solution to my problem.
I will have a deeper look at the rlang documentation.
Best,
Fred.