Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass by argument to dplyr join function within a function?

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

like image 714
Dambo Avatar asked Mar 17 '18 20:03

Dambo


1 Answers

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
like image 66
Tung Avatar answered Nov 01 '22 11:11

Tung