在dplyr中应用基于字符串的过滤器矢量(或字符串矢量) [英] Apply a vector of filters based on a string (or vector of strings) in dplyr
问题描述
R和tidyverse有一些非常强大但同样神秘的方法,可以将字符串转换为可操作的表达式。我觉得需要一位专家才能真正了解如何使用它们。
R and the tidyverse have some extremely powerful but equally mysterious methods for turning strings into actionable expressions. I feel like one needs to be an expert to really understand how to use them.
注意::这个问题与这一个在此,我专门询问一个向量(多个)过滤条件。我演示了一种针对单个过滤器的解决方案,当我尝试将其扩展到多个过滤器的多种方法失败时。
NOTE: this question differs from this one in that I specifically ask about a vector (that is multiple) filter conditions. I demonstrate a solution for single filters that fails when I try multiple ways of extending it to multiple filters.
我想按照以下方式进行操作:
I want to do something along the lines of:
df = data.frame(A=1:10, B=1:10)
df %>% filter(A<3, B<5)
但是其中的过滤器包含在任何字符串中,例如 A< 3,B< 5
或诸如 c( A< 3, B< 5)$ c之类的字符向量$ c>。
But where the filters are contained in either a string such as "A<3, B<5"
or a character vector such as c("A<3", "B<5")
.
我可以做
df %>% filter(eval(str2expression("A<3")))
# A B
# 1 1 1
# 2 2 2
但这不起作用:
df %>% filter(eval(str2expression("A<3, B<5")))
Error in str2expression("A<3, B<5") : <text>:1:4: unexpected ','
1: A<3,
^
这些也不起作用:
> df %>% filter(!!c(str2expression("A<3"), str2expression("B<5")))
Error: Argument 2 filter condition does not evaluate to a logical vector
> df %>% filter(!!!c(str2expression("A<3"), str2expression("B<5")))
Error: Can't splice an object of type `expression` because it is not a vector
Run `rlang::last_error()` to see where the error occurred.
计算 str2expression
中的表达式向量某些原因仅适用最后一个表达式:
Evaluating a vector of expressions from str2expression
for some reason only applies the last expression:
> df %>% filter(eval(c(str2expression("A<3"), str2expression("B<5"))))
# A B
# 1 1 1
# 2 2 2
# 3 3 3
# 4 4 4
使用a计算的表达式向量完全失败:
Using a vector of evaluated expressions fails altogether:
> df %>% filter(!!!c(eval(str2expression("A<3")), eval(str2expression("B<5"))))
Error in eval(str2expression("A<3")) : object 'A' not found
我可以这样做:
> df %>% filter(!!!c(expr(A<3), expr(B<5)))
# A B
# 1 1 1
# 2 2 2
这告诉我 expr(A< 3)
与 str2expression( A< 3)
不同,但这还不是开始
该怎么办?
推荐答案
向@学习罗纳克·沙(Ronak Shah)的答案显然是在dplyr中,我可以在过滤器中使用单个&
而不是逗号来使用多个条件。我完全不理解这一点-与和逻辑不同:
Learning from @Ronak Shah's answer, apparently, in dplyr I can use multiple conditions with a single &
in filter instead of a comma. I don't understand this at all---it is not the same thing as an and logical:
> df %>% filter(A<3 & B<5)
A B
1 1 1
2 2 2
> df %>% filter(A<3 && B<5)
A B
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 6 6
7 7 7
8 8 8
9 9 9
10 10 10
不过,以下方法确实有效:
Nevertheless, the following does work:
> df %>% filter(eval(str2expression("A<3 & B<5")))
A B
1 1 1
2 2 2
> df %>% filter(eval(str2expression("A<6 & B<5")))
A B
1 1 1
2 2 2
3 3 3
4 4 4
这篇关于在dplyr中应用基于字符串的过滤器矢量(或字符串矢量)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!