Finding the start and stop indices in sequence in R

旧时模样 提交于 2019-12-06 09:52:44

Assuming your vector consists of 0 and 1 values:

which(diff(c(0L, x)) == 1L)
#[1] 1 5 7
which(diff(c(x, 0L)) == -1L)
#[1] 2 5 9

Otherwise you'd need something like x <- x == 1L first.

Elegant way is

y <- which(x==1)
startIndx <- y[!(y-1) %in% y]
stopIndex <- y[!(y+1) %in% y]
rbind(startIndx, stopIndex)
#          [,1] [,2] [,3]
#startIndx    1    5    7
#stopIndex    2    5    9

Try this:

y = rle(x)

stopIndex  = with(y, cumsum(lengths)[values==1])
startIndex = stopIndex - with(y, lengths[values==1]) + 1

#> stopIndex
#[1] 2 5 9
#> startIndex
#[1] 1 5 7

What about this? [editted version according to suggestion of alexis_laz]

library(cgwtools)
res <- seqle(which(as.logical(x)))
rbind(res$values, res$values + res$lengths - 1)
     [,1] [,2] [,3]
[1,]    1    5    7
[2,]    2    5    9

How about this:

startIndx<-rev(length(x)-cumsum(rle(rev(x))$lengths)[rle(rev(x))$values==1]+1)
stopIndex<-cumsum(rle(x)$lengths)[rle(x)$values==1]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!