Reputation: 3486
I would like to pass an unquoted variable name x
to a left_join
function. The output I expect is the same as if I ran:
left_join(mtcars, mtcars, by = c('mpg' = 'mpg'))
I'm trying this:
ff <- function(x) {
x <- enquo(x)
left_join(mtcars, mtcars, by = c(x = x))
}
ff(mpg)
Error in match(x, table, nomatch = 0L) : 'match' requires vector arguments
Upvotes: 6
Views: 2348
Reputation: 61
Another way of passing a column into a function where the join happens with "LHS" = "RHS"
could look like this:
data("mtcars")
library(tidyverse)
function_left_join <- function(x) {
mtcars %>%
left_join(mtcars, by = names(select(., {{x}})))
}
head(function_left_join(mpg))
#> mpg cyl.x disp.x hp.x drat.x wt.x qsec.x vs.x am.x gear.x carb.x cyl.y
#> 1 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 6
#> 2 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 6
#> 3 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 6
#> 4 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 6
#> 5 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 4
#> 6 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 4
#> disp.y hp.y drat.y wt.y qsec.y vs.y am.y gear.y carb.y
#> 1 160.0 110 3.90 2.620 16.46 0 1 4 4
#> 2 160.0 110 3.90 2.875 17.02 0 1 4 4
#> 3 160.0 110 3.90 2.620 16.46 0 1 4 4
#> 4 160.0 110 3.90 2.875 17.02 0 1 4 4
#> 5 108.0 93 3.85 2.320 18.61 1 1 4 1
#> 6 140.8 95 3.92 3.150 22.90 1 0 4 2
You can also potentially pass more columns to the select()
function, in order join on multiple keys
Upvotes: 3
Reputation: 28331
You need strings as input for by
therefore you need to use quo_name
break a quosure
and return a string.
library(rlang)
library(tidyverse)
ff <- function(x) {
x <- enquo(x)
left_join(mtcars, mtcars, by = quo_name(x))
}
head(ff(mpg))
#> mpg cyl.x disp.x hp.x drat.x wt.x qsec.x vs.x am.x gear.x carb.x cyl.y
#> 1 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 6
#> 2 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 6
#> 3 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 6
#> 4 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 6
#> 5 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 4
#> 6 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 4
#> disp.y hp.y drat.y wt.y qsec.y vs.y am.y gear.y carb.y
#> 1 160.0 110 3.90 2.620 16.46 0 1 4 4
#> 2 160.0 110 3.90 2.875 17.02 0 1 4 4
#> 3 160.0 110 3.90 2.620 16.46 0 1 4 4
#> 4 160.0 110 3.90 2.875 17.02 0 1 4 4
#> 5 108.0 93 3.85 2.320 18.61 1 1 4 1
#> 6 140.8 95 3.92 3.150 22.90 1 0 4 2
To use x
for both LHS & RHS of by
, we need to use set_names
Credit to this answer
ff2 <- function(x) {
x <- enquo(x)
by = set_names(quo_name(x), quo_name(x))
left_join(mtcars, mtcars, by = by)
}
head(ff2(mpg))
#> mpg cyl.x disp.x hp.x drat.x wt.x qsec.x vs.x am.x gear.x carb.x cyl.y
#> 1 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 6
#> 2 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 6
#> 3 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 6
#> 4 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 6
#> 5 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 4
#> 6 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 4
#> disp.y hp.y drat.y wt.y qsec.y vs.y am.y gear.y carb.y
#> 1 160.0 110 3.90 2.620 16.46 0 1 4 4
#> 2 160.0 110 3.90 2.875 17.02 0 1 4 4
#> 3 160.0 110 3.90 2.620 16.46 0 1 4 4
#> 4 160.0 110 3.90 2.875 17.02 0 1 4 4
#> 5 108.0 93 3.85 2.320 18.61 1 1 4 1
#> 6 140.8 95 3.92 3.150 22.90 1 0 4 2
Upvotes: 8