在 R 中使用带有多个输入和过滤器的 match.fun
我有以下数据库以及几个输入值:
operator <- ">="
amt <- 600
col <- "salary"
data <- data.frame(
emp_id = c (1:5),
emp_name = c("Rick","Dan","Michelle","Ryan","Gary"),
salary = c(623.3,515.2,611.0,729.0,843.25)
)
我使用原始列名使用 match.fun,但想使用“col”值来指定要过滤的列名,类似于它的内容下面:
data %>%
filter(match.fun(operator)(col, amt))
我尝试添加“!!”当它在那里时到 col 的前面,但这不起作用。如果我在上表中将“col”替换为“salary”,这确实有效,但我希望能够动态更改“col”是什么,并使函数对此做出反应。
I have the following database along with a couple of input values:
operator <- ">="
amt <- 600
col <- "salary"
data <- data.frame(
emp_id = c (1:5),
emp_name = c("Rick","Dan","Michelle","Ryan","Gary"),
salary = c(623.3,515.2,611.0,729.0,843.25)
)
I've used match.fun using the original column name, but want to use the "col" value to specify which column name to filter on, similar to what it'd be below:
data %>%
filter(match.fun(operator)(col, amt))
I've tried adding "!!" to the front of col when it's in there, but that doesn't work. If I replace "col" with "salary" in the above table, that does work, but I want to be able to dynamically change what "col" is and have the function react to that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我们可以使用
.data
根据输入选择列col
-output
或转换为
sym
bol 并求值 (!!
)或者另一种选择是将其传递到
across
或者另一种选择是使用
paste/str_c
创建表达式,然后解析We may use
.data
to select the column based on the inputcol
-output
Or convert to
sym
bol and evaluate (!!
)Or another option is to pass it in
across
Or another option is to create an expression with
paste/str_c
and then parse