Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use string as filter in dplyr?

Tags:

r

dplyr

Is there a way to use a string variable as the filter argument in 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.

like image 772
John Richardson Avatar asked Dec 18 '14 19:12

John Richardson


1 Answers

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.

like image 71
Rich Scriven Avatar answered Oct 08 '22 15:10

Rich Scriven