grepl in R to find matches to any of a list of character strings

0 votes

Is it possible to use a grepl argument when referring to a list of values, maybe using the %in% operator? I want to take the data below and if the animal name has "dog" or "cat" in it, I want to return a certain value, say, "keep"; if it doesn't have "dog" or "cat", I want to return "discard".

data <- data.frame(animal = sample(c("cat","dog","bird", 'doggy','kittycat'), 50, replace = T))

Now, if I were just to do this by strictly matching values, say, "cat" and "dog', I could use the following approach:

matches <- c("cat","dog")

data$keep <- ifelse(data$animal %in% matches, "Keep", "Discard")

But using grep or grepl only refers to the first argument in the list:

data$keep <- ifelse(grepl(matches, data$animal), "Keep","Discard")

returns

Warning message:
In grepl(matches, data$animal) :
  argument 'pattern' has length > 1 and only the first element will be used

Note, I saw this thread in my search, but this doesn't appear to work: grep using a character vector with multiple patterns

Jun 22, 2022 in Data Science by avinash
• 1,840 points
2,458 views

1 answer to this question.

0 votes

Inside of a grepl regular expression, you can utilise a "or" (|) statement.

Ifelse(grepl("dog|cat", data$animal), "keep," "discard," # [1] "keep," "keep," "discard," # [9] "keep," "keep," "discard," "keep," "keep," "keep," "keep," "discard," "keep," and "keep" #[17] Discard" Keep" Discard" Keep" Discard" Keep" Discard" Keep" #[25] "keep," "keep," "keep," "keep," "keep," "keep," and "keep" #[33] "keep," "discard," "keep," "discard," "keep," "keep," and "discard" #[41] "keep," "keep," "keep," "keep," "keep," "keep," "keep," "keep," and "discard" are all forms of "keep."
Dog|Cat instructs the regular expression engine to search for either "dog" or "cat," returning matches for both.

Unleash the power of data with our comprehensive Data Science Training.

answered Jun 24, 2022 by Sohail
• 3,040 points

Related Questions In Data Science

0 votes
0 answers

How to manually find the minors of a matrix in R programming?

I have to write a function that ...READ MORE

Jul 5, 2022 in Data Science by avinash
• 1,840 points
305 views
0 votes
0 answers

How do I get a list of built-in data sets in R?

Please advise on how to obtain the ...READ MORE

Jul 5, 2022 in Data Science by avinash
• 1,840 points
184 views
0 votes
0 answers

To speed up the tapply function in R, or another function to convert data frame into a matrix

I must turn a sizable dataset into ...READ MORE

Jun 24, 2022 in Data Science by Sohail
• 3,040 points
204 views
0 votes
0 answers

In R, how to get an object's name after it is sent to a function?

I'm trying to find the opposite of ...READ MORE

Jul 5, 2022 in Data Science by avinash
• 1,840 points
177 views
0 votes
1 answer

Big Data transformations with R

Dear Koushik, Hope you are doing great. You can ...READ MORE

answered Dec 18, 2017 in Data Analytics by Sudhir
• 1,610 points
768 views
0 votes
2 answers

Transforming a key/value string into distinct rows in R

We would start off by loading the ...READ MORE

answered Mar 26, 2018 in Data Analytics by Bharani
• 4,660 points
844 views
0 votes
1 answer

Finding frequency of observations in R

You can use the "dplyr" package to ...READ MORE

answered Mar 26, 2018 in Data Analytics by Bharani
• 4,660 points
5,549 views
0 votes
1 answer

Left Join and Right Join using "dplyr"

The below is the code to perform ...READ MORE

answered Mar 27, 2018 in Data Analytics by Bharani
• 4,660 points
859 views
0 votes
1 answer

How to make loop for one-at-a time logistic regression in R?

You're probably looking for something similar to ...READ MORE

answered Jun 20, 2022 in Data Science by Sohail
• 3,040 points
778 views
0 votes
1 answer

How To Create Vector of Vector In R

Create a list: List() on x returns x[[1]] ...READ MORE

answered Jun 24, 2022 in Data Science by Sohail
• 3,040 points
276 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP