2015-08-28 63 views
5

我有R中的数据帧等所谓UK_profiles滤波器功能在中的R dplyr不工作

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 

使用dplyr我希望使用过滤和删除与grepl行:

require(dplyr) 

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

但是,我收到一个错误,指出找不到对象“名称”。

我还得到:在data.matrix(数据):强制引入的NAs。

对象'名称'显然在数据框中。有人可以帮忙吗?

+4

你加载了'dplyr'包吗?在R中默认有一个'filter'函数,它给出了完全相同的错误。加载'dplyr'('library(dplyr)')后,'filter'函数可以工作。 – 2015-08-28 08:57:17

+3

为什么dplyr? 'UK_profiles [!grepl(“Rollup | Microsite | Mobile | Test | tset | Profile | Facebook | Unfiltered | returnurl”,UK_profiles $ names),]' – zx8754

+0

Hi @Pascal。是的,我确实加载了包谢谢。 – GKyle

回答

30

看起来好像你正在获得stats::filter函数而不是dplyr函数。为确保您得到正确的,请使用符号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 

你甚至都不需要为此做library(dplyr)工作 - 你需要dplyr虽然安装。

这适用于任何包装的功能。

+0

你知道为什么会发生这种情况吗? –