如何使用字符向量过滤数据帧

时间:2019-10-31 09:33:53

标签: r filter dplyr

我正在尝试使用filter()包中的dplyr函数过滤data.frame。这里的主要问题是我想对条件使用向量。

例如

library(dplyr)
conditions <- c("Sepal.Width<3.2","Species==setosa")
DATA <- iris %>%
  filter(conditions) #This doesnt work, of course.

是否有需要的功能

conditions <- c("Sepal.Width<3.2","Species==setosa")

作为输入并给我

Sepal.Width<3.2 & Species==setosa

作为输出?我虽然打算将eval(parse...)sapply一起使用,也许还要和paste0()一起添加&,但不能使其正常工作。

任何帮助都将不胜感激。

3 个答案:

答案 0 :(得分:3)

有多个问题。首先,您需要在第二种情况下用引号引起来:

conditions <- c("Sepal.Width < 3.2", "Species == 'setosa'")

然后,您需要指定两个条件之间的关联。在这里,我假设一个&。然后,您可以使用eval(parse(...))

iris %>%
 filter(eval(parse(text = paste(conditions, sep = "&"))))

   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1           5.1         3.5          1.4         0.2  setosa
2           4.9         3.0          1.4         0.2  setosa
3           4.7         3.2          1.3         0.2  setosa
4           4.6         3.1          1.5         0.2  setosa
5           5.0         3.6          1.4         0.2  setosa
6           5.4         3.9          1.7         0.4  setosa
7           4.6         3.4          1.4         0.3  setosa
8           5.0         3.4          1.5         0.2  setosa
9           4.4         2.9          1.4         0.2  setosa
10          4.9         3.1          1.5         0.1  setosa

另一方面,我认为引用@Martin Mächler来警告与此方法相关的潜在问题始终很重要:

  

(可能)唯一的连接是通过parse(text = ....),一切都很好   R程序员应该知道,这很少有效或安全   表示构造表达式(或调用)的方法。而是了解更多有关   replace(),quote()以及可能的使用权   do.call(替代...)。

答案 1 :(得分:3)

这是一种方式:

conditions <- c("Sepal.Width<3.2","Species=='setosa'")
# note the small change here:               ↑      ↑
DATA <- iris %>%
  filter(eval(parse(text = paste(conditions, collapse = "&"))))

> DATA
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1           4.9         3.0          1.4         0.2  setosa
2           4.6         3.1          1.5         0.2  setosa
3           4.4         2.9          1.4         0.2  setosa
4           4.9         3.1          1.5         0.1  setosa
5           4.8         3.0          1.4         0.1  setosa
6           4.3         3.0          1.1         0.1  setosa
7           5.0         3.0          1.6         0.2  setosa
8           4.8         3.1          1.6         0.2  setosa
9           4.9         3.1          1.5         0.2  setosa
10          4.4         3.0          1.3         0.2  setosa
11          4.5         2.3          1.3         0.3  setosa
12          4.8         3.0          1.4         0.3  setosa

答案 2 :(得分:2)

一种整理方法是使用rlang::parse_exprs()

library(dplyr)

conditions <- c("Sepal.Width < 3.2", "Species == 'setosa'")

iris %>%
  filter( !!! rlang::parse_exprs(conditions))

   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1           4.9         3.0          1.4         0.2  setosa
2           4.6         3.1          1.5         0.2  setosa
3           4.4         2.9          1.4         0.2  setosa
4           4.9         3.1          1.5         0.1  setosa
5           4.8         3.0          1.4         0.1  setosa
6           4.3         3.0          1.1         0.1  setosa
7           5.0         3.0          1.6         0.2  setosa
8           4.8         3.1          1.6         0.2  setosa
9           4.9         3.1          1.5         0.2  setosa
10          4.4         3.0          1.3         0.2  setosa
11          4.5         2.3          1.3         0.3  setosa
12          4.8         3.0          1.4         0.3  setosa
相关问题