0

So if I have the following

list <- c("catdog","chicken","poop")
names <- c("Fabio","John","Jack")
df <- data.frame(names, list, stringsAsFactors=FALSE)

       names   list
1      Fabio catdog
2       John    cat
3       Jack    dog

Assuming list is a column of strings. I want to know how can I return rows where "cat" AND "dog" after appearing once as a pair they may appear more times. I tried:

want <- c("cat","dog")
df[grepl(paste(want,collapse="&"),df$list),]

I know this works with "|" for some reason its not working with "&". Let me know if anyone can help with this. Thanks!

theamateurdataanalyst
  • 2,794
  • 4
  • 38
  • 72

1 Answers1

0

This is an option, if neither 'cat' nor 'dog' can repeat within a single string.

df[grepl('(cat)|(dog).*(\\1|\\2)', df$list), ]
Matthew Plourde
  • 43,932
  • 7
  • 96
  • 113