Learn R Programming

DTSg (version 0.6.0)

colapply.DTSg: Apply Function Column-wise

Description

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

Usage

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

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.

clone

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

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.

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.

Value

Returns a DTSg object.

Details

In addition to the argument, this method 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.

See Also

DTSg, function, cols, TALFs, aggregate, list, POSIXct, difftime, interpolateLinear

Examples

Run this code
# NOT RUN {
# 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 = function(x, ...) {cumsum(x)}, funby = byYm____)

## S3 method
colapply(x = x, fun = function(x, ...) {cumsum(x)}, 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"]
  )
}

# }

Run the code above in your browser using DataLab