0

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.

4
  • 1
    L[grep(paste(id1,id2,sep=""),L)] L[grep(paste(id2,id1,sep=""),L)] Commented Mar 8, 2016 at 23:42
  • 2
    A simple non-regex solution could be grepl(id1, L) & grepl(id2, L). You can add fixed = TRUE to both if efficiency is important. Commented Mar 8, 2016 at 23:44
  • I don't understand it, but apparently grepl("(dog(cat)?)", L) works courtesy of stackoverflow.com/questions/1177081/… Commented Mar 9, 2016 at 0:32
  • That pattern matches dogcat but also dog, as in: grepl("(dog(cat)?)", "dog"). Commented Mar 9, 2016 at 1:09

1 Answer 1

2

The error is from misplaced parentheses, so these minor variations on your code will work.

L[grep(paste(id1,id2,sep=""), L)]
# character(0)
L[grep(paste(id2,id1,sep=""), L)]
# [1] "catdog"

Alternatively this is a regex one-liner:

L[grep(paste0(id2, id1, "|", id1, id2), L)]
# [1] "catdog"

That and some patterns in the comments will also match dogcatt. To avoid this you could use ^ and $ like so:

x <- c("dogcat", "foo", "catdog", "ddogcatt")
x[grep(paste0("^", id2, id1, "|", id1, id2, "$"), x)]
# [1] "dogcat" "catdog"
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.