backtrace_values: Backtrace values

Description Usage Arguments Details Value Examples

Description

backtrace_values() traces a chain of entries with the same value backwards through a vector.

Usage

1
backtrace_values(x, backtrace_val, starts)

Arguments

x

A vector in which a value is to be traced backwards.

backtrace_val

The value to be traced.

starts

An integer vector of the same length as x, with starting points of a backwards chain being 1 and all other entries being 0. As returned by detect_transitions

Details

Supplied with a vector starts of starting points (e.g. as returned by detect_transitions()), this function traces a value backtrace_val backwards within a vector x. This defines a chain of entries whose value is equal to the traced value. The output may be coerced to a logical vector and used for indexing the original sequence (see examples).

Value

An integer vector of the same length as x, with values of 1 if the entry belongs to a chain, and 0 otherwise.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# Determine the start of a "Dead" to "Alive" transition for a sequence of
# mortality measurements
x <- c("Alive", "Almost dead", "Dead", "Dead", "Dead", "Alive", "Dead",
            "Dead", "Alive", "Dead", "Cut and Resprout", "Dead", "Alive")
da_starts <- detect_transitions(x, c("Dead", "Alive"))

# Backtrace all values equal to "Dead"
backtrace_values(x, "Dead", da_starts)

# Extract positions of the traced values
trace <- as.logical(backtrace_values(x, "Dead", da_starts))
which(trace)
x[trace]

dschoenig/IDENTcc documentation built on May 16, 2019, 4:07 a.m.