【发布时间】:2018-08-03 20:42:14
【问题描述】:
我有一个带有成对变量的小标题:
- a_x, a_y,
- b_x, b_y,
- c_x, c_y 等等。
如何编写一个根据“a”、“b”或“c”进行过滤的函数。比如我想返回
filter(df, a_x != a_y)
或
filter(df, b_x != b_y)
我正在使用 quosures,如中所述 https://dplyr.tidyverse.org/articles/programming.html, 但没有成功。
这是一个例子:
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")
【问题讨论】: