Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

! grep in R - finding items that do not match [duplicate]

Tags:

r

I want to find rows in a dataframe that do not match a pattern.

 Key = c(1,2,3,4,5)
 Code = c("X348","I605","B777","I609","F123")
 df1 <- data.frame(Key, Code)

I can find items beginning with I60 using:

 df2 <- subset (df1, grepl("^I60", df1$Code))

But I want to be able to find all the other rows (that is, those NOT beginning with I60). The invert argument does not work with grepl. grep on its own does not find all rows, nor can it pass the results to the subset command. Grateful for help.

like image 278
Stewart Wiseman Avatar asked Jan 22 '15 10:01

Stewart Wiseman


Video Answer


2 Answers

You could use the [ operator and do

df1[!grepl("I60", Code),]

(Suggested clarification from @Hugh:) Another way would be

df1[!grepl("I60",df1$Code),]

Here is the reference manual on array indexing, which is done with [:

http://cran.r-project.org/doc/manuals/R-intro.html#Array-indexing

like image 136
Scott C Wilson Avatar answered Oct 19 '22 08:10

Scott C Wilson


Also, you can try this:

 Key = c(1,2,3,4,5)
Code = c("X348","I605","B777","I609","F123")
df1 <- data.frame(Key, Code)
toRemove<-grep("^I60", df1$Code)
df2 <- df1[-toRemove,]
like image 31
Fedorenko Kristina Avatar answered Oct 19 '22 08:10

Fedorenko Kristina