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
我会做这样的事情:
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:通过更改sum
为which
你将获得每个实例的开始.
归档时间: |
|
查看次数: |
1173 次 |
最近记录: |