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 in Data Science by avinash
• 860 points
7 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.
answered 6 days ago by Sohail
• 2,960 points

Related Questions In Data Science

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

6 days ago in Data Science by Sohail
• 2,960 points
2 views
0 votes
0 answers

Introduction to Statistical Learning with Applications in R Figure Codes

I recently bought the following book: An Introduction ...READ MORE

Jun 1 in Data Science by avinash
• 860 points
20 views
0 votes
1 answer

Why is it not advisable to use attach() in R, and what should I use instead?

There is one more option that applies ...READ MORE

answered 6 days ago in Data Science by Sohail
• 2,960 points
7 views
0 votes
0 answers

how to make 2 separate substrings from strsplit in R

When it comes to accessing the elements ...READ MORE

Jun 13 in Data Science by Avinash
• 1,260 points
3 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
422 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
467 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
4,287 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
492 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 in Data Science by Sohail
• 2,960 points
19 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 6 days ago in Data Science by Sohail
• 2,960 points
11 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP