Skip to contents

Applies circ_difference() along a vector, comparing each angle with the one lag positions before it — the turn from one heading to the next, rather than the difference between two angles you name. Unlike base::diff() the result is the same length as x, padded with NA at the start, so it can be used inside dplyr::mutate().

Usage

circ_successive_difference(x, lag = 1L)

Arguments

x

A numeric vector of angles, in radians.

lag

A positive integer (default 1L) giving the lag to difference at.

Value

A numeric vector the same length as x, in radians. The first lag entries are NA; the rest are angular differences in (-pi, pi].

See also

Examples

circ_successive_difference(c(0, pi / 2, pi, 3 * pi / 2))
#> [1]       NA 1.570796 1.570796 1.570796

# crossing zero is a small step, not a large one
circ_successive_difference(c(6.2, 0.1))
#> [1]        NA 0.1831853