Get Location of Start/End Points

Usage

start_end(x)

Arguments

x
A vector of 1 and 0 or logical.

Value

Returns a two column data.frame of start and end locations for ones.

Description

Get the locations of start/end places for the ones in a binary vector.

Examples

set.seed(10); (x <- sample(0:1, 50, TRUE, c(.35, .65)))
[1] 1 1 1 0 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 0 0 1 0 1 1 0 0 1 0 1 1 1 1 0 1 0 0 0 0 1 1 1 1 0 1 1 1 1 1 0
start_end(x)
start end 1 1 3 2 5 10 3 12 19 4 22 22 5 24 25 6 28 28 7 30 33 8 35 35 9 40 43 10 45 49
(y <- sample(c(TRUE, FALSE), 50, TRUE, c(.35, .65)))
[1] FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE [21] FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE [41] FALSE TRUE FALSE FALSE FALSE TRUE FALSE FALSE TRUE FALSE
start_end(y)
start end 1 2 2 2 15 16 3 28 28 4 31 31 5 37 37 6 42 42 7 46 46 8 49 49

Author

Roland (http://stackoverflow.com/users/1412059/roland) and Tyler Rinker .