Finds the positions in a vector where the value changes an returns the "index
regions", i.e. the regions between indices starts_at
and
ends_at
whithin each of which the value does not change, i.e.
length(unique(x[starts_at:ends_at])) == 1L
is TRUE
.
Value
data frame with one row more than there are value changes in
x
. If the value in x
does not change from one index to the
next, it is assumed to belong to the same index region. If the value
changes, a new index region begins. In the result data frame each index
region is given by starts_at
and ends_at
which are the
indices of the first and last element, respectively, of each index region.
The function returns NULL
for input vectors x
of length 0.
Details
The input vector x
must not contain any NA
because it is not
clear how to handle this. The function stops with an error if there are any
NA
in x
.
Examples
findChanges(c(1,2,2,3,3,3))
#> starts_at ends_at value
#> 1 1 1 1
#> 2 2 3 2
#> 3 4 6 3
findChanges(c("a", "a", "b", "c", "c", "d"))
#> starts_at ends_at value
#> 1 1 2 a
#> 2 3 3 b
#> 3 4 5 c
#> 4 6 6 d
findChanges(c(TRUE, TRUE, TRUE, FALSE, FALSE, TRUE, FALSE, FALSE))
#> starts_at ends_at value
#> 1 1 3 TRUE
#> 2 4 5 FALSE
#> 3 6 6 TRUE
#> 4 7 8 FALSE