dplyr: filter where two columns in data.frame are equal

Viewed 9548

In base R one can easily filter to rows where two columns are equals like so:

mtcars[mtcars$cyl==mtcars$carb,]

Using dplyr's filter this can be done easily

mtcars %>% filter(cyl==carb)

But if I am writing a function using this code I would want to use filter_, but this code doesn't work

mtcars %>% filter_("cyl"=="carb")

Since in this case it thinks "carb" is a value to test rather than a variable.

My question is how can you use filter_ to compare two variables in a data.frame?

2 Answers
Related