Counting all the matchings of a pattern in a vector in R

I have a boolean vector in which I want to count the number of occurrences of some patterns.

For example, for the pattern "(1,1)" and the vector "(1,1,1,0,1,1,1)", the answer should be 4.

The only built-in function I found to help is grepRaw, which finds the occurrences of a particular string in a longer string. However, it seems to fail when the sub-strings matching the pattern overlap:

length(grepRaw("11","1110111",all=TRUE))
# [1] 2

Do you have any ideas to obtain the right answer in this case?

Edit 1

I'm afraid that Rich's answer works for the particular example I posted, but fails in a more general setting:

> sum(duplicated(rbind(c(FALSE,FALSE),embed(c(TRUE,TRUE,TRUE,FALSE,TRUE,TRUE,TRUE),2)))) [1] 3

In this other example, the expected answer would be 0.

Upvotes: 1

Views: 236

Answers (1)

G. Cocca
G. Cocca

Reputation: 2541

Using the function rollapply you can apply a moving window of width = 2 summing the values. Then you can sum the records where the result is equal to 2 i.e. sum(c(1,1))

 library(zoo)

 z <- c(1,1,1,0,1,1,1)
 sum(rollapply(z, 2, sum) == 2)

Upvotes: 1

Related Questions