我有大约180000个看起来像这样的数据点 - Figure1。它的0和5是不规则的间隔。
data<- 0,0,0,5,5,5,0,0,0,5,5,5,0,0.. and so on.
我想找到起始和结束y值为5的索引(标记为蓝色的索引)。该图附于图中。我正在使用R进行分析。我尝试使用cpt.meanvar
包中的changepoint
函数,但它并未提供所有更改点位置。还有另一种方法吗?
答案 0 :(得分:2)
#DATA
mydata<- c(0,0,0,5,5,5,0,0,0,5,5,5,0,0)
#Find out which indices have 5
v = which(mydata == 5)
#Split into groups of consecutive integers and get the range for each sub-group
lapply(split(v, cumsum(c(1, diff(v) != 1))), function(x) range(x))
#$`1`
#[1] 4 6
#$`2`
#[1] 10 12
放入data.frame
可能会更容易
setNames(data.frame(do.call(rbind, lapply(split(v, cumsum(c(1, diff(v) != 1))),
function(x) range(x)))),
c("Start", "End"))
# Start End
#1 4 6
#2 10 12