Skip to contents

Creates “events” from vector x of values based on changes in the value of consecutive elements in x.

Usage

eventsOnChange(x, numberOnly = FALSE, include.value = FALSE)

Arguments

x

vector containing elements to be grouped into “events”

numberOnly

if TRUE, only the number of “events” is returned instead of a data frame containing first and last index of each “event”.

include.value

if TRUE and numberOnly is FALSE, the returned data frame will contain a column value containing the value that was found in each index section between iBeg and iEnd.

Value

Per default (numberOnly = FALSE) a data frame is returned with as many rows as “events” were found in vector x. As long as the value in x does not change from one index to the next, it is assumed to belong to the same event. If the value changes, a new event begins. In the result data frame each event is represented by iBeg and iEnd which are the indices of the first and last element, respectively, in x that build the event. If numberOnly is TRUE the number of “events” is returned, that is one plus the number of changes in the value of x from its first to its last element.

See also

Examples

eventsOnChange(c(1,2,2,3,4,4,4,5))
#>   iBeg iEnd
#> 1    1    1
#> 2    2    3
#> 3    4    4
#> 4    5    7
#> 5    8    8
  
# Ouput: list of five events, i.e. there are four changes of 
#        the value in the given vector.
#
#   iBeg iEnd
# 1    1    1
# 2    2    3
# 3    4    4
# 4    5    7
# 5    8    8
  
eventsOnChange(c(1, 2, 2, 3, 4, 4, 4, 5), numberOnly = TRUE) ## 5 (events)
#> [1] 5