- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在评论者建议!! ensym的地方回答这个question,我认为这可能是使用 curl {{但我无法使其正常工作(也许不适用?)的好地方。
在不使用filter_,eval/parse或quote-unquote的情况下,如何执行此过滤器操作?会帮忙吗?
我的解决方案(1g)使用filter_和使用粘贴建立的条件。
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
最佳答案
正如莱昂内尔(Lionel)指出的那样,curl-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
关于R如何使用带有filter或filter_的curl curl?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57702596/
我想过滤数据框以仅保留基于所选列的完整案例。使用 NSE 很容易做到这一点 filter() : library(dplyr) dd % filter(complete.cases(var1, var
我想创建一个应用于数据框的过滤器列表。就像是: filters = list(cyl=4, am=1) 然后将其应用于 'mtcars' 数据框,以获取 cyl=4 和 am=1 的记录。我可以做这个
我想使用在变量中定义的字段过滤数据框,以选择也在变量中的值。说我有 df % filter_(.dots = ~ fld == sval) 什么都没有返回。我得到了我想要的 df %>% filter
我正在使用 ffmpeg-python包装。 我想通过这个包装器模仿这个 ffmpeg 命令。 ffmpeg -i multiaudio.avi -map 0:a:0 multiaudio_test_
我正在使用 ffmpeg-python包装。 我想使用 crop过滤器以提取视频文件的裁剪部分;我希望该部分的大小是输入尺寸的一半。 根据ffmpeg的文档,我可以使用输入参数in_w和 in_h像这
我正在尝试编写一个使用 dplyr 计算 z 的所有唯一值的函数。当我有实际命名为 z 的变量时,我的函数工作正常。但是,如果变量名为 x,则会出现错误(在代码下方)。 test.data%
我是一名优秀的程序员,十分优秀!