简体   繁体   English

在 dplyr 中使用字符串作为过滤器?

[英]Use string as filter in dplyr?

Is there a way to use a string variable as the filter argument in dplyr?有没有办法在 dplyr 中使用字符串变量作为过滤器参数? For example:例如:

filter(iris,Sepal.Length > 6)

would be replaced with将被替换为

string <- 'Sepal.Length > 6'
filter(iris,string)

Basically, I am looking for the entire filter string to be a variable as I am creating the filter string pragmatically.基本上,我正在寻找整个过滤器字符串作为变量,因为我正在以务实的方式创建过滤器字符串。 Thanks for any help.谢谢你的帮助。

If you want to filter with a string argument, you'll need to use filter_() instead of filter()如果要使用字符串参数进行过滤,则需要使用filter_()而不是filter()

string <- 'Sepal.Length > 6'
filter_(iris, string)

Also note that it's recommended to use the *_() functions when programming.另请注意,建议在编程时使用*_()函数。

This is the only way I have found that works in the new tidyval:这是我发现在新 tidyval 中有效的唯一方法:

> s = "A<3"
> data.frame(A=1:10, B=1:10) %>% filter(eval(str2expression(s)))
  A B
1 1 1
2 2 2

However, I have not figured out how to do this with multiple conditions .但是,我还没有想出如何在多个条件下做到这一点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM