I have two elements:
id1 <- "dog"
id2 <- "cat"
I want to extract any combination of these elements (dogcat or catddog) from a vector
L <- c("gdoaaa","gdobbb","gfoaaa","ghobbb","catdog")
L
I tried:
L[grep(paste(id1,id2,sep="")),L]
L[grep(paste(id2,id1,sep="")),L]
but this gives an error.
I would be grateful for your help in correcting the above.
L[grep(paste(id1,id2,sep=""),L)] L[grep(paste(id2,id1,sep=""),L)]grepl(id1, L) & grepl(id2, L). You can addfixed = TRUEto both if efficiency is important.grepl("(dog(cat)?)", L)works courtesy of stackoverflow.com/questions/1177081/…dogcatbut alsodog, as in:grepl("(dog(cat)?)", "dog").