rg4s
rg4s

Reputation: 897

How do I delete a row with a pattern in R?

I have a dataframe where I want to delete all rows with specific pattern. I am confused with compiling a regular expression.

Data:

structure(list(id = 1:5, email = c("[email protected]", "[email protected]", 
"[email protected]", "[email protected]", "[email protected]")), class = "data.frame", row.names = c(NA, 
-5L))

What I am trying to do is:

data <- data %>%
  filter(email != "[email protected]")

But something is wrong with my regex. What is the most effective way to compose a regular expression for such patterns? What is the proper regex pattern for my sample case?

Upvotes: 3

Views: 1909

Answers (2)

Chris Ruehlemann
Chris Ruehlemann

Reputation: 21400

In base R you can remove the rows in which the pattern @pattern.com is detected by the function greplin the email column:

data[-which(grepl("@pattern.com", data$email)),]
  id       email
1  1 [email protected]
2  2 [email protected]
3  3 [email protected]

Data:

data <- structure(list(id = 1:5, email = c("[email protected]", "[email protected]", 
                                           "[email protected]", "[email protected]", "[email protected]")), class = "data.frame", row.names = c(NA, 

Upvotes: 1

manotheshark
manotheshark

Reputation: 4357

This uses grepl to perform a regex comparison

libary(dplyr)

data %>%
  filter(!grepl("@pattern.com$", email))

  id       email
1  1 [email protected]
2  2 [email protected]
3  3 [email protected]

Upvotes: 3

Related Questions