Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
417 views
in Technique[技术] by (71.8m points)

filter - 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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

62 comments

56.7k users

...