Learn R Programming

runner (version 0.2.1)

streak_run: Running streak length

Description

Calculates running series of consecutive elements

Usage

streak_run(x, k = 0L, na_rm = TRUE, na_pad = FALSE, idx = 1L)

Arguments

x

vector of any type where running streak is calculated

k

running window size. By default window size equals length(x). Allow varying window size specified by vector of length(x)

na_rm

logical (default na_rm=TRUE) - if TRUE NA are replaced by last observed streak prior to element.

na_pad

logical (default na_pad=FALSE) - if TRUE first k-results will be filled by NA. If k is not specified na_pad=F by default.

idx

an optional integer vector containing indexes numbers of observation.

Value

numeric vector of length equals length of x containing running streak length in k-long window.

Examples

Run this code
# NOT RUN {
set.seed(11)
x1 <- sample(c("a","b"),15,replace=TRUE)
x2 <- sample(c(NA_character_,"a","b"),15,replace=TRUE)
k <- sample(1:4,15,replace=TRUE)
streak_run(x1) # simple streak run
streak_run(x1, k=2) # streak run within 2-element window
streak_run(x2, na_pad=TRUE, k=3) # streak run within k=3 with padding NA
streak_run(x1, k=k) # streak run within varying window size specified by vector k
# }

Run the code above in your browser using DataLab