You could try the following:
b[!(+(apply(sapply(a, function(x) grepl(x,b)),1,sum)) > 0)]
[1] "zyc" "ulk" "mae"
'Peeling' this previous call from the inside, the results are the following: First, obtain a matrix of matches from the grepl:
call (with sapply
):
sapply(a, function(x) grepl(x,b))
# da ba cs dd ek
#[1,] FALSE FALSE FALSE FALSE FALSE
#[2,] FALSE FALSE FALSE FALSE FALSE
#[3,] FALSE FALSE FALSE FALSE FALSE
#[4,] FALSE FALSE TRUE FALSE FALSE
#[5,] FALSE FALSE FALSE TRUE FALSE
#[6,] TRUE FALSE FALSE FALSE FALSE
Note that the columns are the elements of a
and the rows are the elements of b
.
Then, apply
the function sum per rows (in R
, TRUE is 1 and FALSE is 0:
apply(sapply(a, function(x) grepl(x,b)),1,sum)
#[1] 0 0 0 1 1 1
Note that here, the row sums might be > 1 (if there is more than 1 match), so it must be coerced into a logical with the previous call wrapped around:
+() > 0
With this, we can match ([
) the indices of b, but since we want the opposite, we use the operator !
.
#full code:
step.one <- sapply(a, function(x) grepl(x,b))
step.two <- apply(step.one,1,sum)
step.three <- +(step.two > 0)
step.four <- !step.three
#finally:
b[step.four]
As David shows in the comments, this is a much more elegant approach:
b[-which(sapply(a, grepl, b), arr.ind = TRUE)[, "row"]]