Dplyr 加入 by=(a = b),其中 a 和 b 是包含字符串的变量? [英] Dplyr join on by=(a = b), where a and b are variables containing strings?

查看:17
本文介绍了Dplyr 加入 by=(a = b),其中 a 和 b 是包含字符串的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 dplyr 执行两个表的内部连接,我想我被非标准评估规则绊倒了.当使用 by=("a" = "b") 参数时,当 "a" 和 "b" 是实际字符串时,一切都按预期工作.这是一个有效的玩具示例:

I am trying to perform an inner join two tables using dplyr, and I think I'm getting tripped up by non-standard evaluation rules. When using the by=("a" = "b") argument, everything works as expected when "a" and "b" are actual strings. Here's a toy example that works:

library(dplyr)
data(iris)

inner_join(iris, iris, by=c("Sepal.Length" = "Sepal.Width"))

但是假设我将 inner_join 放入一个函数中:

But let's say I was putting inner_join in a function:

library(dplyr)
data(iris)

myfn <- function(xname, yname) {
    data(iris)
    inner_join(iris, iris, by=c(xname = yname))
}

myfn("Sepal.Length", "Sepal.Width")

这将返回以下错误:

错误:无法加入列 'xname' x 'Sepal.Width':索引超出范围

我怀疑我可以用一些花哨的表达方式来完成这项工作,包括解析、引用或取消引用,但我对这些细节有点不清楚.

I suspect there is some fancy expression, deparsing, quoting, or unquoting that I could do to make this work, but I'm a bit murky on those details.

推荐答案

您可以使用

myfn <- function(xname, yname) {
    data(iris)
    inner_join(iris, iris, by=setNames(yname, xname))
}

by = c("a"="b")   # same as by = c(a="b")

有点误导,因为这两个值都不是正确的字符值.您实际上创建了一个命名的字符向量.动态设置等号左边的值与右边的不同.您可以使用 setNames() 动态设置向量的名称.

is slightly misleading because both those values aren't proper character values. You're actually created a named character vector. To dynamically set the values to the left of the equals sign is different from those on the right. You can use setNames() to set the names of the vector dynamically.

这篇关于Dplyr 加入 by=(a = b),其中 a 和 b 是包含字符串的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆