问题描述
我有一个带有成对变量的小标题:
- a_x, a_y,
- b_x, b_y,
- c_x、c_y 等等.
I have a tibble with paired variables:
- a_x, a_y,
- b_x, b_y,
- c_x, c_y and so on.
如何编写根据a"、b"或c"进行过滤的函数.例如我想返回
How can I write a function that filters depending on "a", "b" or "c". For instance I want to return
filter(df, a_x != a_y)
或
filter(df, b_x != b_y)
我正在使用 quosures,如中所述https://dplyr.tidyverse.org/articles/programming.html,但没有成功.
I'm using quosures, as described in https://dplyr.tidyverse.org/articles/programming.html, but without success.
示例如下:
test <-tribble(~a_x, ~b_x, ~a_y, ~b_y,
1,2,1,2,
5,6,5,8,
9,8,11,8)
# that works
x <-quo(a_x)
y <-quo(a_y)
filter(test, !!x == !!y)
x <-quo(b_x)
y <-quo(b_y)
filter(test, !!x == !!y)
# but the function doesn't work
my <- function(df, var){
a <- paste0(quo_name(var), "_x")
b <- paste0(quo_name(var), "_y")
print(quo(filter(df, !!a == !!b)))
return(filter(df, !!a == !!b))
}
my(test, "a")
my(test, "b")
推荐答案
当我们传递一个字符串时,更容易转换为符号并求值
As we are passing a string, it is easier to convert to symbol and evaluate
library(dplyr)
library(rlang)
my <- function(df, var){
a <- sym(paste0(var, "_x"))
b <- sym(paste0(var, "_y"))
df %>%
filter(!!a == !!b)
}
my(test, "a")
# A tibble: 2 x 4
# a_x b_x a_y b_y
# <dbl> <dbl> <dbl> <dbl>
#1 1 2 1 2
#2 5 6 5 8
my(test, "b")
# A tibble: 2 x 4
# a_x b_x a_y b_y
# <dbl> <dbl> <dbl> <dbl>
#1 1 2 1 2
#2 9 8 11 8
如果 OP 也打算传递不带引号的参数,
If the OP intends to pass unquoted arguments as well,
my <- function(df, var){
a <- sym(paste0(quo_name(enquo(var)), "_x"))
b <- sym(paste0(quo_name(enquo(var)), "_y"))
df %>%
filter(!!a == !!b)
}
my(test, a)
my(test, b)
注意:以上采用带引号和不带引号的参数
NOTE: The above takes both quoted and unquoted arguments
identical(my(test, "a"), my(test, a))
#[1] TRUE
identical(my(test, "b"), my(test, b))
#[1] TRUE
这篇关于使用引用在 tidyverse 中编程过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!