Delete rows containing specific strings in R

180,681

Solution 1

This should do the trick:

df[- grep("REVERSE", df$Name),]

Or a safer version would be:

df[!grepl("REVERSE", df$Name),]

Solution 2

You could use dplyr::filter() and negate a grepl() match:

library(dplyr)

df %>% 
  filter(!grepl('REVERSE', Name))

Or with dplyr::filter() and negating a stringr::str_detect() match:

library(stringr)

df %>% 
  filter(!str_detect(Name, 'REVERSE'))

Solution 3

Actually I would use:

df[ grep("REVERSE", df$Name, invert = TRUE) , ]

This will avoid deleting all of the records if the desired search word is not contained in any of the rows.

Solution 4

You can use stri_detect_fixed function from stringi package

stri_detect_fixed(c("REVERSE223","GENJJS"),"REVERSE")
[1]  TRUE FALSE

Solution 5

You can use this function if it's multiple string df[!grepl("REVERSE|GENJJS", df$Name),]

Share:
180,681
user3091668
Author by

user3091668

Updated on July 05, 2022

Comments

  • user3091668
    user3091668 almost 2 years

    I would like to exclude lines containing a string "REVERSE", but my lines do not match exactly with the word, just contain it.

    My input data frame:

       Value   Name 
        55     REVERSE223   
        22     GENJJS
        33     REVERSE456
        44     GENJKI
    

    My expected output:

       Value   Name 
        22     GENJJS
        44     GENJKI
    
  • Jason Melo Hall
    Jason Melo Hall over 7 years
    What do you mean by "safer"?
  • nemja
    nemja almost 7 years
    What if I want to delete the rows containing a "(". The following does not seem to work: df[!grepl("(", df$Name),]
  • cuttlefish
    cuttlefish over 6 years
    @nemja The grepl function uses regular expressions for the match, which have a syntax where ( is meaningful. If you set the named parameter fixed = TRUE then grepl will perform a literal match without using regular expressions, which should work for your use case.
  • micstr
    micstr almost 6 years
    This question asks for many strings. So what happens if you want to remove multiple strings i.e. remove.list <- c("REVERSE", "FOO", "BAR, "JJ")
  • sbha
    sbha almost 6 years
    Sure, you can do create the list like this: remove.list <- paste(c("REVERSE", "FOO", "BAR", "JJ"), collapse = '|') And then filter like this: df %>% filter(!grepl(remove.list, Name)) df %>% filter(!str_detect(Name, remove.list))
  • shantanu pathak
    shantanu pathak almost 6 years
    @JasonMeloHall minus (-) operator does use negative indexing and negation (!) operator uses logical indexing so negation operator is safer than minus(-)
  • KNN
    KNN over 5 years
    How could you modify this to also delete the row above the row that contains the matching string?
  • Simone
    Simone almost 5 years
    Thanks a lot - googled for about 1h before I found your answer. Works brilliant.
  • ML33M
    ML33M over 4 years
    Hi guys, I tried the code in my contigency table making, although I save my dataframe as a new data, the filtered rows are still kept as a level with "0" entries. how do I ask it to completely delete the rows in the new dataframe?
  • MarceloRibeiro
    MarceloRibeiro almost 2 years
    How to drop rows based in strings present in two columns, like filter(!grepl('REVERSE', Name & 'BAR', Name)), I got the following error trying this way. ! operations are possible only for numeric, logical or complex types