Reputation: 679
I have a data frame in R like 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 wish to filter
and delete rows with grepl
using:
require(dplyr)
UK_profiles.filtered <- filter(UK_profiles, !grepl("Rollup|Microsite|Mobile|Test|tset|Profile|Facebook|Unfiltered|returnurl", name))
However, I get an error saying:
object 'name' not found.
I also get:
In data.matrix(data) : NAs introduced by coercion.
The object 'name'
is clearly in the dataframe. Can someone please help?
Upvotes: 33
Views: 102167
Reputation: 1
It needs dplyr::
before the code if you run filter
as dplyr
:
dplyr::filter(UK_profiles, !grepl("Rollup|Microsite|Mobile|Test|tset|Profile|Facebook|Unfiltered|returnurl", name))
Upvotes: 0
Reputation: 52238
I had the same problem when I unloaded tidyverse libraries and later tried to use filter()
without first loading the tidyverse.
So the simple solution for me was to run this:
library(tidyverse)
or this would have worked too
library(dplyr)
and everything worked as expected again.
Upvotes: 0
Reputation: 1106
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
Upvotes: 9
Reputation: 11
I think you need to both install the dplyr
package with install.packages("dplyr")
and then use the library
command library(dplyr)
to load dplyr
into memory for use.
For example the mtcars
dataset is a part of dplyr
, if I only install dplyr
and then enter head(mtcars)
it doesn't find it. Once I use the library
command it is found.
Upvotes: -1
Reputation: 94182
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.
Upvotes: 80