本文介绍了如何将参数传递给函数内的 dplyr 连接函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将一个不带引号的变量名 x
传递给一个 left_join
函数.我期望的输出与我运行时相同:
left_join(mtcars, mtcars, by = c('mpg' = 'mpg'))
我正在尝试:
ff <- function(x) {x <- enquo(x)left_join(mtcars, mtcars, by = c(x = x))}ff(mpg)
匹配错误(x,表,nomatch = 0L):匹配"需要向量论据
解决方案
您需要字符串作为 by
的输入,因此您需要使用 quo_name
打破 quosure
并返回一个字符串.
库(rlang)图书馆(tidyverse)ff <- 函数(x) {x <- enquo(x)left_join(mtcars,mtcars,by = quo_name(x))}头(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
对 LHS & 使用 x
by
的RHS,我们需要使用set_names
归功于此答案
ff2
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)
解决方案
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
这篇关于如何将参数传递给函数内的 dplyr 连接函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!