I have a regular expressions/string replacement dilemma (sorry if this is a duplicate post, I was looking for a solution and couldn't find one, but please let me know if I just missed a similar post!).
We have a dataset that's structured in two columns: subject and verb. I want to delete every modal auxiliary in the verb column, but only if the modal is with another word. So I want to replace "can" in the string "can do" with "", but I don't want to replace "can" if it appears on its own. I thought I could maybe use an ifelse statement, like in the code below:
all_doubles <- all_doubles %>%
mutate(modal_removed = ifelse(str_detect(all_doubles$verb_lemma, modal_with_words) == TRUE,
str_replace_all("can|could|may|might|shall|should|will|would|need", ""),
all_doubles$verb_lemma))
But I'm having trouble getting the regex right to return only modal auxiliaries that are accompanied by other words. Right now, I'm using this but it doesn't seem to be working well:
modal_with_words <- ".+can|could|may|might|shall|should|will|would|need.+"
Any advice would be very appreciated (I'm sure there's a better way to do this)! Thank you very much!