How to count a repeating repeating part of a sequence in R?

use*_*964 4 r count sequence repeat

Is it possible to count a repeating part of a sequence in R? For example:

x<- c(1,3.0,3.1,3.2,1,1,2,3.0,3.1,3.2,4,4,5,6,5,3.0,3.1,3.2,
      3.1,2,1,4,6,4.0,4,3.0,3.1,3.2,5,3.2,3.0,4)
Run Code Online (Sandbox Code Playgroud)

Is it possible to count the times that the subsequence 3.0,3.1,3.2 occurs? So in this example it must be: 4

Aru*_*run 5

我会做这样的事情:

pattern <- c(3, 3.1, 3.2)
len1 <- seq_len(length(x) - length(pattern) + 1)
len2 <- seq_len(length(pattern))-1
sum(colSums(matrix(x[outer(len1, len2, '+')], 
     ncol=length(len1), byrow=TRUE) == pattern) == length(len2))
Run Code Online (Sandbox Code Playgroud)

PS:通过更改sumwhich你将获得每个实例的开始.