Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete rows containing specific strings in R

Tags:

string

r

match

rows

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 
like image 541
user3091668 Avatar asked Mar 07 '14 12:03

user3091668


People also ask

How do you delete a row that contains a specific value in R?

First of all, create a data frame. Then, use single square subsetting with apply function to remove rows that contains a specific number.

How do I remove rows from two conditions in R?

To remove rows of data from a dataframe based on multiple conditional statements. We use square brackets [ ] with the dataframe and put multiple conditional statements along with AND or OR operator inside it. This slices the dataframe and removes all the rows that do not satisfy the given conditions.


1 Answers

This should do the trick:

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

Or a safer version would be:

df[!grepl("REVERSE", df$Name),] 
like image 190
Pop Avatar answered Sep 19 '22 13:09

Pop