mov_avg: Moving average windows

Description Usage Arguments Value Examples

View source: R/ff_mov_avg.R

Description

Smooth numeric series with a moving average windows.

Usage

1
2
3
4
5
6
7
8
9
mov_avg(
  x,
  col_name = "last",
  k,
  pos = "c",
  out_name = NULL,
  from = NULL,
  to = NULL
)

Arguments

x

data frame with class Date or POSIXct in the first column and numeric on the others.

col_name

string vector with the column(s) name(s) of the series to smooth. The default value uses the 'last' column. Another single string choice is to use 'all'. Is important to keep in mind that this argument commands, so if you provide two columns names, k and pos arguments must be of length two; if not the single value will be recycled.

k

numeric vector with the windows size. E.g.: k = 5.

pos

string vector with the position of the windows:

  • 'c': center (default). The output value is in the middle of the window.

  • 'l': left aligned. The output value is on the left, so the function weights the (k - 1) values at the right side.

  • 'r': right aligned. The output value is on the right, so the function weights the (k - 1) values at the left side.

out_name

optional. String vector with new column names. If you set it as NULL the function will overwrite the original series.

from

optional. String value for 'Date' class or POSIXct class for date-time data containing the starting Date.

to

optional. String value for 'Date' class or POSIXct class for date-time data containing the ending Date.

Value

The same data frame but with the smooth series.

Examples

1
2
3
4
5
6
7
8
# read guido daily streamflow records
path <- system.file('extdata', 'snih_qd_guido.xlsx',
        package = 'hydrotoolbox')

# read and apply the function
qd_guido <-
  read_snih(path = path, by = 'day', out_name = 'q(m3/s)') %>%
  mov_avg(k = 5, out_name = 'q_smooth')

hydrotoolbox documentation built on July 9, 2021, 1:06 a.m.