使用字符串作为dplyr中的过滤器?
问题描述:
是否可以使用字符串变量作为dplyr中的过滤器参数?例如:
Is there a way to use a string variable as the filter argument in dplyr? For example:
filter(iris,Sepal.Length > 6)
将替换为
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.
答
如果要使用字符串参数进行过滤,则需要使用 filter _()
而不是 filter()
If you want to filter with a string argument, you'll need to use filter_()
instead of filter()
string <- 'Sepal.Length > 6'
filter_(iris, string)
也请注意,建议使用 * _()函数。
Also note that it's recommended to use the *_()
functions when programming.