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
The grepl() stands for “grep logical”. In R it is a built-in function that searches for matches of a string or string vector. The grepl() method takes a pattern and data and returns TRUE if a string contains the pattern, otherwise FALSE.
The grep and grepl functions use regular expressions or literal values as patterns to conduct pattern matching on a character vector. The grep returns indices of matched items or matched items themselves while grepl returns a logical vector with TRUE to represent a match and FALSE otherwise.
Example 2: Apply grep & grepl with Multiple PatternsWe can also use grep and grepl to check for multiple character patterns in our vector of character strings. We simply need to insert an |-operator between the patterns we want to search for.
You can use an "or" (|) statement inside the regular expression of grepl.
ifelse(grepl("dog|cat", data$animal), "keep", "discard") # [1] "keep" "keep" "discard" "keep" "keep" "keep" "keep" "discard" # [9] "keep" "keep" "keep" "keep" "keep" "keep" "discard" "keep" #[17] "discard" "keep" "keep" "discard" "keep" "keep" "discard" "keep" #[25] "keep" "keep" "keep" "keep" "keep" "keep" "keep" "keep" #[33] "keep" "discard" "keep" "discard" "keep" "discard" "keep" "keep" #[41] "keep" "keep" "keep" "keep" "keep" "keep" "keep" "keep" #[49] "keep" "discard" The regular expression dog|cat tells the regular expression engine to look for either "dog" or "cat", and return the matches for both.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With