2017-07-18 22 views
0

我有大約180000個數據點,看起來像這樣 - Figure1。它的0和5以不規則的間隔。查找條件y值的x軸位置

data<- 0,0,0,5,5,5,0,0,0,5,5,5,0,0.. and so on. 

我想找到開始和結束的y值是5(用藍色標記)的索引。圖中附有圖。我正在使用R進行分析。我嘗試使用changepoint軟件包中的cpt.meanvar函數,但它並未提供所有更改點位置。有另一種方法嗎?

+0

這是不太清楚。你的'data'變量似乎是y,但是x是什麼?索引?上述一小部分數據的答案是什麼?它會是開始= c(4,10)結束= c(6,12)? – G5W

回答

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 
+0

非常感謝!這正是我正在尋找的 – ACE