如何在使用filter或filter_时使用双大括号(curly curly)?

10
我正在回答这个问题,评论者建议使用!!ensym,我认为这可能是使用双大括号{{}}的好地方,但我无法使其起作用(也许不适用?)。
如果不使用filter_、eval/parse或引用-引用,该如何执行此过滤操作?会使用~吗?
我的解决方案(1g)使用filter_和通过paste构建的条件。1a可行(但是否可以以某种方式使用{{}}?)
如果我们想按多个变量进行过滤怎么办?这就是你在下面看到2g起作用的地方(而2a不再起作用)。
library(tidyverse)
set.seed(1234)
A <- matrix(rnorm(30),nrow = 10, ncol = 3) %>% as_tibble() %>% set_names(paste("var", seq(1:3), sep = ""))
varnames_1 <- c("var2")

(expected_result_1 <- filter(A, var2 > 0))
#> # A tibble: 3 x 3
#>     var1   var2   var3
#>    <dbl>  <dbl>  <dbl>
#> 1 -2.35  0.0645  0.460
#> 2  0.429 0.959  -0.694
#> 3 -0.890 2.42   -0.936

(answer_1a <- filter(A,!!ensym(varnames_1) > 0)) # works (thanks joran and aosmith)
#> # A tibble: 3 x 3
#>     var1   var2   var3
#>    <dbl>  <dbl>  <dbl>
#> 1 -2.35  0.0645  0.460
#> 2  0.429 0.959  -0.694
#> 3 -0.890 2.42   -0.936

(answer_1b <- filter_(A, varnames_1 > 0)) # filter_ not doing what I thought it might
#> Warning: filter_() is deprecated. 
#> Please use filter() instead
#> 
#> The 'programming' vignette or the tidyeval book can help you
#> to program with filter() : https://tidyeval.tidyverse.org
#> This warning is displayed once per session.
#> # A tibble: 10 x 3
#>      var1    var2    var3
#>     <dbl>   <dbl>   <dbl>
#>  1 -1.21  -0.477   0.134 
#>  2  0.277 -0.998  -0.491 
#>  3  1.08  -0.776  -0.441 
#>  4 -2.35   0.0645  0.460 
#>  5  0.429  0.959  -0.694 
#>  6  0.506 -0.110  -1.45  
#>  7 -0.575 -0.511   0.575 
#>  8 -0.547 -0.911  -1.02  
#>  9 -0.564 -0.837  -0.0151
#> 10 -0.890  2.42   -0.936

(answer_1c <- filter(A, {{varnames_1}} > 0)) # curly curly not doing what I thought it might
#> # A tibble: 10 x 3
#>      var1    var2    var3
#>     <dbl>   <dbl>   <dbl>
#>  1 -1.21  -0.477   0.134 
#>  2  0.277 -0.998  -0.491 
#>  3  1.08  -0.776  -0.441 
#>  4 -2.35   0.0645  0.460 
#>  5  0.429  0.959  -0.694 
#>  6  0.506 -0.110  -1.45  
#>  7 -0.575 -0.511   0.575 
#>  8 -0.547 -0.911  -1.02  
#>  9 -0.564 -0.837  -0.0151
#> 10 -0.890  2.42   -0.936
(answer_1d <- filter(A, {{varnames_1 > 0}})) # curly curly not doing what I thought it might
#> `arg` must be a symbol

conditions_1 <- paste(varnames_1, "> 0")
(answer_1e <- filter(A, conditions_1)) # does not work
#> Error: Argument 2 filter condition does not evaluate to a logical vector
(answer_1f <- filter(A, {{conditions_1}})) # curly curly not doing what I thought it might
#> Error: Argument 2 filter condition does not evaluate to a logical vector
(answer_1g <- filter_(A, conditions_1)) # works
#> # A tibble: 3 x 3
#>     var1   var2   var3
#>    <dbl>  <dbl>  <dbl>
#> 1 -2.35  0.0645  0.460
#> 2  0.429 0.959  -0.694
#> 3 -0.890 2.42   -0.936

# what if we wanted to filter multiple variables?

varnames_2 <- c("var2", "var3")
(expected_result_2 <- filter(A, var2 > 0 & var3 > 0))
#> # A tibble: 1 x 3
#>    var1   var2  var3
#>   <dbl>  <dbl> <dbl>
#> 1 -2.35 0.0645 0.460

(answer_2a <- filter(A,!!ensym(varnames_2) > 0)) # does not work
#> Only strings can be converted to symbols

conditions_2 <- paste(paste(varnames_2, "> 0"), collapse = " & ")
(answer_2f <- filter(A, {{conditions_2}})) # curly curly not doing what I thought it might
#> Error: Argument 2 filter condition does not evaluate to a logical vector
(answer_2g <- filter_(A, conditions_2)) # works
#> # A tibble: 1 x 3
#>    var1   var2  var3
#>   <dbl>  <dbl> <dbl>
#> 1 -2.35 0.0645 0.460

使用reprex package (v0.3.0)于2019-08-28创建

3个回答

14

{{只能在函数内部与函数参数一起使用。顺便说一下,ensym()和其他以en开头的运算符也是如此。

如果您不在函数中并且将变量名称作为字符串,则需要使用!!sym()sym()部分将变量名称转换为代码对象(符号),!!部分将其插入到原处。


6
正如Lionel所指出的,curly-curly在函数内部可用。要将其与filter一起使用,因此必须将调用包装在函数内部。
f <- function(.df, v) { 
  filter(.df, {{ v }} > 0) 
}

# Curly-curly provides automatic NSE support
f( A, var2 )
# # A tibble: 3 x 3
#     var1   var2   var3
#    <dbl>  <dbl>  <dbl>
# 1 -2.35  0.0645  0.460
# 2  0.429 0.959  -0.694
# 3 -0.890 2.42   -0.936

# Strings have to be first converted to symbols
f( A, !!sym("var3") )
# # A tibble: 3 x 3
#     var1    var2  var3
#    <dbl>   <dbl> <dbl>
# 1 -1.21  -0.477  0.134
# 2 -2.35   0.0645 0.460
# 3 -0.575 -0.511  0.575

Curly-curly是用来引用单个参数的。你可以通过使用purrr::reduce进行顺序应用,将其扩展为适用于多个变量。(不要忘记首先将你的字符串转换为实际的变量名!):

syms(varnames_2) %>% reduce(f, .init=A)
# # A tibble: 1 x 3
#    var1   var2  var3
#   <dbl>  <dbl> <dbl>
# 1 -2.35 0.0645 0.460

对于函数f,你也可以使用v <- as.name(v) - Jorge Paredes

2
如果paste(paste(varnames_2, "> 0"), collapse = " & ")是主要问题,那么您需要构建过滤参数。最初的回答。
library(tidyverse)
library(rlang)

set.seed(1234)
A <- matrix(rnorm(30),nrow = 10, ncol = 3) %>% as_tibble() %>% set_names(paste("var", seq(1:3), sep = ""))

# with variables as arguments
filter_gt0 <- function(d, ...) {
  conds <- ensyms(...)
  conds <- map(conds, ~quo(!!.x > 0))  
  d %>%
    filter(!!!conds)
}
A %>%
  filter_gt0(var2, var3)
# # A tibble: 1 x 3
# var1   var2  var3
# <dbl>  <dbl> <dbl>
#   1 -2.35 0.0645 0.460

# or with variables as input
conds <- quos(var2, var3)
filter_gt0_2 <- function(d, conds) {
  conds <- map(conds, ~quo(!!.x > 0))  
  d %>%
    filter(!!!conds)
}
A %>%
  filter_gt0_2(conds)
# # A tibble: 1 x 3
# var1   var2  var3
# <dbl>  <dbl> <dbl>
#   1 -2.35 0.0645 0.460

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接