findChanges | R Documentation |
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
.
findChanges(x)
x |
vector of atomic mode (e.g. logical, numeric, character) |
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
.
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.
findChanges(c(1,2,2,3,3,3))
findChanges(c("a", "a", "b", "c", "c", "d"))
findChanges(c(TRUE, TRUE, TRUE, FALSE, FALSE, TRUE, FALSE, FALSE))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.