add_moving_average: Add Moving Average for an Arbitrary Number of Intervals

Description Usage Arguments Details Value Examples

View source: R/add_moving_average.R

Description

The functions adds moving average for an arbitrary number of intervals. The data can be returned sorted or according to the original order.

Usage

1
2
3
4
5
6
7
8
add_moving_average(
  .data,
  sort_cols,
  val,
  intervals = 2,
  res_val = "{{val}}_mavg",
  restore_order = FALSE
)

Arguments

.data

A tibble or data frame.

sort_cols

Columns used for sorting passed in a manner consistent with arrange

val

Column used to calculate moving average passed as bare column name or a character string.

intervals

A number of intervals for moving average.

res_val

Resulting moving average, defaults to name of val suffixed with _mavg.

restore_order

A logical, defaults to FALSE if TRUE it will restore original data order.

Details

The function can be used independently or within dplyr pipeline.

Value

A tibble with appended moving average.

Examples

1
add_moving_average(mtcars, sort_cols = c("mpg", "cyl"), val = hp, intervals = 2)

konradedgar/KEmisc documentation built on April 15, 2021, 1:50 p.m.