可能很容易解决此问题(一如既往...),但看不到它。 我想使用包含在字符串中的列名和逻辑运算符的条件来过滤数据框。
df0 显示了一个最小的示例。
在 df_normal_filter 中,我只在过滤器函数中写条件。
在 df_quote_filter 中,我首先引用表达式,然后取消引用。
但是如何使用存储为字符串(condition_string)的条件来获得相同的结果?
谢谢! :)
library(tidyverse)
condition_string <- "V1 != V2 & V1 != V3"
condition_quote <- quote(V1 != V2 & V1 != V3)
df0 <- tibble(
V1 = c(5,6,7,8,9),
V2 = c(5,4,7,1,2),
V3 = c(3,5,1,8,9)
)
df0
#> # A tibble: 5 x 3
#> V1 V2 V3
#> <dbl> <dbl> <dbl>
#> 1 5 5 3
#> 2 6 4 5
#> 3 7 7 1
#> 4 8 1 8
#> 5 9 2 9
df_normal_filter <- df0 %>%
filter(V1 != V2 & V1 != V3)
df_normal_filter
#> # A tibble: 1 x 3
#> V1 V2 V3
#> <dbl> <dbl> <dbl>
#> 1 6 4 5
df_quote_filter <- df0 %>%
filter(!!condition_quote)
df_quote_filter
#> # A tibble: 1 x 3
#> V1 V2 V3
#> <dbl> <dbl> <dbl>
#> 1 6 4 5
由reprex package(v0.3.0)于2020-05-13创建