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),]
Author by
user3091668
Updated on July 05, 2022Comments
-
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 over 7 yearsWhat do you mean by "safer"?
-
nemja almost 7 yearsWhat if I want to delete the rows containing a "(". The following does not seem to work: df[!grepl("(", df$Name),]
-
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 parameterfixed = TRUE
thengrepl
will perform a literal match without using regular expressions, which should work for your use case. -
micstr almost 6 yearsThis 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 almost 6 yearsSure, 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 almost 6 years@JasonMeloHall minus (-) operator does use negative indexing and negation (!) operator uses logical indexing so negation operator is safer than minus(-)
-
KNN over 5 yearsHow could you modify this to also delete the row above the row that contains the matching string?
-
Simone almost 5 yearsThanks a lot - googled for about 1h before I found your answer. Works brilliant.
-
ML33M over 4 yearsHi 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 almost 2 yearsHow 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