在 R 中使用带有多个输入和过滤器的 match.fun

发布于 2025-01-13 14:25:07 字数 491 浏览 1 评论 0原文

我有以下数据库以及几个输入值:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

诠释孤独 2025-01-20 14:25:07

我们可以使用 .data 根据输入选择列 col

library(dplyr)
data %>% 
   filter(match.fun(operator)(.data[[col]], amt))

-output

   emp_id emp_name salary
1      1     Rick 623.30
2      3 Michelle 611.00
3      4     Ryan 729.00
4      5     Gary 843.25

或转换为 symbol 并求值 (!!)

data %>% 
 filter(match.fun(operator)(!! rlang::sym(col), amt))
  emp_id emp_name salary
1      1     Rick 623.30
2      3 Michelle 611.00
3      4     Ryan 729.00
4      5     Gary 843.25

或者另一种选择是将其传递到 across

data %>% 
   filter(across(all_of(col), ~ match.fun(operator)(.x, amt)))
  emp_id emp_name salary
1      1     Rick 623.30
2      3 Michelle 611.00
3      4     Ryan 729.00
4      5     Gary 843.25

或者另一种选择是使用 paste/str_c 创建表达式,然后解析

library(stringr)
data %>% 
  filter(!!rlang::parse_expr(str_c(col, operator, amt)))
  emp_id emp_name salary
1      1     Rick 623.30
2      3 Michelle 611.00
3      4     Ryan 729.00
4      5     Gary 843.25

We may use .data to select the column based on the input col

library(dplyr)
data %>% 
   filter(match.fun(operator)(.data[[col]], amt))

-output

   emp_id emp_name salary
1      1     Rick 623.30
2      3 Michelle 611.00
3      4     Ryan 729.00
4      5     Gary 843.25

Or convert to symbol and evaluate (!!)

data %>% 
 filter(match.fun(operator)(!! rlang::sym(col), amt))
  emp_id emp_name salary
1      1     Rick 623.30
2      3 Michelle 611.00
3      4     Ryan 729.00
4      5     Gary 843.25

Or another option is to pass it in across

data %>% 
   filter(across(all_of(col), ~ match.fun(operator)(.x, amt)))
  emp_id emp_name salary
1      1     Rick 623.30
2      3 Michelle 611.00
3      4     Ryan 729.00
4      5     Gary 843.25

Or another option is to create an expression with paste/str_c and then parse

library(stringr)
data %>% 
  filter(!!rlang::parse_expr(str_c(col, operator, amt)))
  emp_id emp_name salary
1      1     Rick 623.30
2      3 Michelle 611.00
3      4     Ryan 729.00
4      5     Gary 843.25
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文