I'm attempting to use mutate and grepl to modify a column based on specific conditions in 2 other columns. One of those conditions is met by matching specific words in one of the columns. Both conditions have to be met. Below is a description of script example of what I'm trying to do:
df <- x %>%
mutate(col3 = ifelse(grepl(c("string1"), col1) & grepl(c("string5|string6|string7"), col2),
"new_string",
col1))
When using grepl, OR (|) seems to be the only thing that works. I need both conditions to be true. I therefore need to use AND (&) or something of the sort.
Anyone might have a simple solution?
(Note: this is a provisional answer. OP needs to provide more information.)
The code you have works, provided it matches the data you have. It would be helpful, as Quinten has suggested, if you provided sample data and an expected output. But, to answer the question, of how to use "mutate and grepl with multiple conditions", literally the way you did it is the answer:
Output: