Delete rows containing specific strings in R

RStringMatchRows

R Problem Overview


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

R Solutions


Solution 1 - R

This should do the trick:

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

Or a safer version would be:

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

Solution 2 - R

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 - R

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 - R

You can use stri_detect_fixed function from stringi package

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

Solution 5 - R

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

Solution 6 - R

You can use it in the same datafram (df) using the previously provided code

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

or you might assign a different name to the datafram using this code

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser3091668View Question on Stackoverflow
Solution 1 - RPopView Answer on Stackoverflow
Solution 2 - RsbhaView Answer on Stackoverflow
Solution 3 - RBobD59View Answer on Stackoverflow
Solution 4 - RbartektartanusView Answer on Stackoverflow
Solution 5 - Rhidden-layerView Answer on Stackoverflow
Solution 6 - RMohamed RahoumaView Answer on Stackoverflow