filter function in dplyr errors

It does seem like you are getting the stats::filter function and not the dplyr one. To make sure you get the right one, use the notation dplyr::filter.

d = data.frame(x=1:10,
 name=c("foo","bar","baz","bar","bar","baz","fnord","qar","qux","quux"))

filter(d, !grepl("ar|ux", name))
Error in grepl("ar|ux", name) : object 'name' not found

dplyr::filter(d, !grepl("ar|ux", name))
  x  name
1 1   foo
2 3   baz
3 6   baz
4 7 fnord

You don't even need to do library(dplyr) for this to work - you do need dplyr installed though.

This works for functions from any package.


To understand why this happens you can recreate the error quite straightforwardly by following these steps.

Load dplyr

Load dplyr into a new session with only default libraries loaded, filter will work as dplyr loaded after stats

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

filter(mtcars, mpg < 15)
#>    mpg cyl disp  hp drat    wt  qsec vs am gear carb
#> 1 14.3   8  360 245 3.21 3.570 15.84  0  0    3    4
#> 2 10.4   8  472 205 2.93 5.250 17.98  0  0    3    4
#> 3 10.4   8  460 215 3.00 5.424 17.82  0  0    3    4
#> 4 14.7   8  440 230 3.23 5.345 17.42  0  0    3    4
#> 5 13.3   8  350 245 3.73 3.840 15.41  0  0    3    4

Unload dplyr

This causes the error as now trying to use stats::filter. By unloading stats we see another error that there is no function called filter found at all

detach("package:dplyr")  # Unload dplyr
filter(mtcars, mpg < 15)  # Using stats::filter
#> Error in filter(., mpg < 15): object 'mpg' not found

detach("package:stats")  # Unload stats

filter(mtcars, mpg < 15)    
#> Error in filter(., mpg < 15): could not find function "filter"

Reload stats and dplyr

Make sure to reload dplyr after stats and we see that the dplyr version of filter works again

library(stats)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> 
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

filter(mtcars, mpg < 15)
#>    mpg cyl disp  hp drat    wt  qsec vs am gear carb
#> 1 14.3   8  360 245 3.21 3.570 15.84  0  0    3    4
#> 2 10.4   8  472 205 2.93 5.250 17.98  0  0    3    4
#> 3 10.4   8  460 215 3.00 5.424 17.82  0  0    3    4
#> 4 14.7   8  440 230 3.23 5.345 17.42  0  0    3    4
#> 5 13.3   8  350 245 3.73 3.840 15.41  0  0    3    4

I reinstalled rlang package with restart session and it helped

Tags:

R

Dplyr