findChanges: Find Value Changes in a Vector

View source: R/findChanges.R

findChangesR Documentation

Find Value Changes in a Vector

Description

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.

Usage

findChanges(x)

Arguments

x

vector of atomic mode (e.g. logical, numeric, character)

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.

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.

Examples

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))


KWB-R/kwb.utils documentation built on April 1, 2024, 7:12 a.m.