Reputation: 55
I want to find sequence of elements in vector that do not match a pattern. For example:
pattern <- c(1,2,3,4)
test <- c(5,4,3,6,1,2,3,4,5,3,3,2,1,2,3,4,6,3,7,5,1,2,3,6)
I want to find whole sequence in "test" that do not match "pattern" or indexes where this situation occurs. So I want to get a result similar to this:
> want
[[1]]
[1] 5 4 3 6
[[2]]
[1] 5 3 3 2
[[3]]
[1] 6 3 7 5 1 2 3 6
or something like this:
> indexes
[1] 1 9 17
Do you have an idea how to do this?
Upvotes: 1
Views: 68
Reputation: 887118
One option is
lapply(scan(text=gsub(paste(pattern,collapse=""), ",",
paste(test, collapse="")), what="", sep=",", quiet = TRUE),
function(x) as.numeric(unlist(strsplit(x, ""))))
#[[1]]
#[1] 5 4 3 6
#[[2]]
#[1] 5 3 3 2
#[[3]]
#[1] 6 3 7 5 1 2 3 6
Upvotes: 2