Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

colapply.DTSg

Apply Function Column-wise


Description

Applies an arbitrary function to selected columns of a DTSg object.

Usage

## S3 method for class 'DTSg'
colapply(
  x,
  fun,
  ...,
  cols = self$cols(class = "numeric")[1L],
  resultCols = NULL,
  suffix = NULL,
  helpers = TRUE,
  funby = NULL,
  ignoreDST = FALSE,
  multiplier = 1L,
  funbyHelpers = NULL,
  clone = getOption("DTSgClone")
)

Arguments

x

A DTSg object (S3 method only).

fun

A function. Its return value must be of length one.

...

Further arguments passed on to fun.

cols

A character vector specifying the columns to apply fun to.

resultCols

An optional character vector of the same length as cols. Non-existing columns specified in this argument are added and existing columns are overwritten by the return values of fun. Columns are matched element-wise between resultCols and cols.

suffix

An optional character string. The return values of fun are added as new columns with names consisting of the columns specified in cols and this suffix. Existing columns are never overwritten. Only used when resultCols is not specified.

helpers

A logical specifying if helper data shall be handed over to fun. See details for further information.

funby

One of the temporal aggregation level functions described in TALFs or a user defined temporal aggregation level function. Can be used to apply functions like cumsum to a certain temporal level. See examples and aggregate for further information.

ignoreDST

A logical specifying if day saving time is ignored during formation of the temporal level. See aggregate for further information.

multiplier

A positive integerish value “multiplying” the temporal aggregation level of certain TALFs. See aggregate for further information.

funbyHelpers

An optional list with helper data passed on to funby. See aggregate for further information.

clone

A logical specifying if the object is modified in place or if a clone (copy) is made beforehand.

Details

In addition to the ... argument, this method optionally hands over a list argument with helper data called .helpers to fun. .helpers contains the following named elements:

  • .dateTime: A POSIXct vector containing the .dateTime column.

  • periodicity: Same as periodicity field. See DTSg for further information.

  • minLag: A difftime object containing the minimum time difference between two subsequent timestamps.

  • maxLag: A difftime object containing the maximum time difference between two subsequent timestamps.

Value

Returns a DTSg object.

See Also

Examples

# new DTSg object
x <- DTSg$new(values = flow)

# linear interpolation of missing values
## R6 method
x$colapply(fun = interpolateLinear)

## S3 method
colapply(x = x, fun = interpolateLinear)

# daily cumulative sums per month
## R6 method
x$colapply(fun = cumsum, helpers = FALSE, funby = byYm____)

## S3 method
colapply(x = x, fun = cumsum, helpers = FALSE, funby = byYm____)

# calculate moving averages with the help of 'runner' (all four given
# approaches provide the same result with explicitly missing timestamps)
if (requireNamespace("runner", quietly = TRUE) &&
    packageVersion("runner") >= numeric_version("0.3.5")) {
  wrapper <- function(..., .helpers) {
    runner::runner(..., idx = .helpers[[".dateTime"]])
  }

  ## R6 method
  x$colapply(fun = runner::runner, f = mean, k = 5       , lag = -2       )
  x$colapply(fun = wrapper       , f = mean, k = "5 days", lag = "-2 days")
  x$colapply(
    fun = runner::runner,
    f = mean,
    k = "5 days",
    lag = "-2 days",
    idx = x$getCol(col = ".dateTime")
  )
  x$colapply(
    fun = runner::runner,
    f = mean,
    k = "5 days",
    lag = "-2 days",
    idx = x[".dateTime"]
  )

  ## S3 method
  colapply(x = x, fun = runner::runner, f = mean, k = 5       , lag = -2       )
  colapply(x = x, fun = wrapper       , f = mean, k = "5 days", lag = "-2 days")
  colapply(
    x = x,
    fun = runner::runner,
    f = mean,
    k = "5 days",
    lag = "-2 days",
    idx = getCol(x = x, col = ".dateTime")
  )
  colapply(
    x = x,
    fun = runner::runner,
    f = mean,
    k = "5 days",
    lag = "-2 days",
    idx = x[".dateTime"]
  )
}

DTSg

A Class for Working with Time Series Based on 'data.table' and 'R6' with Largely Optional Reference Semantics

v0.7.0
MIT + file LICENSE
Authors
Gerold Hepp [aut, cre]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.