Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
422 views
in Technique[技术] by (71.8m points)

r - filter function in dplyr errors

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...