roll_fun: Rolling functions

View source: R/ff_roll_fun.R

roll_funR Documentation

Rolling functions

Description

It provides a generic function to rolling table columns. Internally it is using rollapplyr from package zoo.

Usage

roll_fun(
  x,
  col_name = "last",
  k,
  pos = "c",
  FUN,
  ...,
  out_name = NULL,
  from = NULL,
  to = NULL
)

Arguments

x

data frame (or tibble) with class Date or POSIX* in the first column.

col_name

string vector with the column(s) name(s) of the series to roll. 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.

FUN

the function to be applied.

...

optional arguments to FUN.

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 POSIX* class for date-time data containing the starting Date.

to

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

Value

The same table but with the rolling series.

Examples


# 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)') %>%
  roll_fun(k = 5, FUN = mean, na.rm = TRUE,
   out_name = 'q_smooth')




hydrotoolbox documentation built on April 14, 2023, 12:34 a.m.