rolling_percentile: Function to calculate a rolling percentile from a vector.

View source: R/rolling_percentile.R

rolling_percentileR Documentation

Function to calculate a rolling percentile from a vector.

Description

Function to calculate a rolling percentile from a vector.

Usage

rolling_percentile(
  x,
  width,
  align = "center",
  probs = 0.5,
  na.rm = TRUE,
  round = NA
)

Arguments

x

Numeric vector

width

Width of rolling window

align

Should the index of the result be center (default), left or right aligned compared to the rolling window of observations?

probs

Percentile to calculate

na.rm

Should NAs be removed before the percentile is computed?

round

Number of digits to round result to

Value

Numeric vector of same length as x

Author(s)

Shona Wilde

Examples


# define a vector - this is a daily time series
x <- c(41, 36, 12, 18, 28, 23, 19, 8, 7, 16, 11, 14, 18, 14, 34, 6, 30, 11)

# calculate rolling 7-day 99th percentile 
rolling_percentile(
 x,
 width = 7,
 probs = 0.99
)

# round to 1 decimal place only
rolling_percentile(
 x,
 width = 7,
 probs = 0.99,
 round = 1
)#' 


shonawilde/shonarrr documentation built on Jan. 29, 2025, 1:27 a.m.