dplyr error filter function

I have a data frame in R, such as the so-called UK_profiles :

 row.names id name 1 1 8131437 Profile 2 2 8131719 WolverineCompetition 3 4 8132011 www.vaseline.com 4 10 23265829 www.keepingskinamazing.co.uk 5 23 8042743 Mobile 6 24 8043312 Test 7 25 90914664 Join Our Core 8 26 45272695 UDF 9 27 50547829 apps.euro-bureau.eu/fairathon 10 28 50916438 www.benjerry.intashop.com/ 11 44 83667343 All Web Site Data 12 45 84556272 UK 

Using dplyr, I want to filter and delete lines with grepl :

 require(dplyr) UK_profiles.filtered <- filter(UK_profiles, !grepl("Rollup|Microsite|Mobile|Test|tset|Profile|Facebook|Unfiltered|returnurl", name)) 

However, I get the error message:

object name not found

I also get:

In data.matrix (data): NA are entered by force.

The 'name' object is obviously in the dataframe. Can someone please help?

+11
source share
5 answers

It seems that you are getting the stats::filter function, not dplyr . To make sure you are correct, use the dplyr::filter notation.

 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 make library(dplyr) for this to work - you really need dplyr .

This works for functions from any package.

+44
source

To understand why this is happening, you can simply recreate the error by following these steps.

Download 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 an error since it is now trying to use stats::filter . When unloading stats we see another error: no functions were found with the name filter

 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 statistics and dplyr

Be sure to restart dplyr after stats and we will see that the filter dplyr version is dplyr 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 
+2
source

I reinstalled the Rlang package with restarting the session and it helped

+1
source

Thanks for the solution. But in my case, I ran into a problem that is rather strange when I just say the filter (df, city == "mysore"), but it returns 0 rows.

0
source

I think you need how to install the dplyr package with install.packages("dplyr") and then use the library library(dplyr) to load dplyr into memory for use.
For example, the mtcars is part of dplyr , if I only install dplyr and then head(mtcars) it will not find it. As soon as I use the library command it is found.

-1
source

All Articles