2015-08-28 30 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

你知道爲什麼會發生這種情況嗎? –