| colapply.DTSg {DTSg} | R Documentation |
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,
funbyApproach = self$funbyApproach,
clone = getOption("DTSgClone")
)
Arguments
x |
A |
fun |
A |
... |
Further arguments passed on to |
cols |
A character vector specifying the columns to apply |
resultCols |
An optional character vector of the same length as |
suffix |
An optional character string. The return values of |
helpers |
A logical specifying if helper data shall be handed over to
|
funby |
One of the temporal aggregation level functions described in
|
ignoreDST |
A logical specifying if day saving time shall be ignored
by |
multiplier |
A positive integerish value “multiplying” the
temporal aggregation level of certain |
funbyHelpers |
An optional |
funbyApproach |
A character string specifying the flavour of the applied
temporal aggregation level function. Either |
clone |
A logical specifying if the object shall be modified in place or if a deep clone (copy) shall be made beforehand. |
Value
Returns a DTSg object.
Helper data
In addition to the ... argument, this method optionally hands over a
list argument with helper data called .helpers to fun. This list
contains the following elements:
-
.dateTime: A
POSIXctvector containing the .dateTime column. -
periodicity: Same as the
periodicityfield. -
minLag: A
difftimeobject containing the minimum time difference between two subsequent timestamps. -
maxLag: A
difftimeobject containing the maximum time difference between two subsequent timestamps.
User defined TALFs, TALFs helper data and multiplier
User defined temporal aggregation level functions have to return a
POSIXct vector of the same length as the time series and accept two
arguments: a POSIXct vector as its first and a list with helper data
as its second. The default elements of this list are as follows:
-
timezone: Same as the
timezonefield. -
ignoreDST: Same as the
ignoreDSTargument. -
periodicity: Same as the
periodicityfield. -
na.status: Same as the
na.statusfield. -
multiplier: Same as the
multiplierargument. -
funbyApproach: Same as the
funbyApproachargument.
Any additional element specified in the funbyHelpers argument is appended
to the end of the helper data list. In case funbyHelpers contains an
ignoreDST, multiplier or funbyApproach element, it takes precedence over
the respective method argument. timezone, periodicity and na.status
elements are rejected, as they are always taken directly from the object.
The temporal aggregation level of certain TALFs can be adjusted with the
help of the multiplier argument. A multiplier of 10, for example, makes
byY_____ aggregate to decades instead of years. Another example
is a multiplier of 6 provided to by_m____. The function
then aggregates all months of all first and all months of all second half
years instead of all months of all years separately. This feature is
supported by the following TALFs of the package:
-
byYmdH__(UTC and equivalent as well as all Etc/GMT time zones only) -
by___H__(UTC and equivalent as well as all Etc/GMT time zones only)
Ignore day saving time
ignoreDST tells a temporal aggregation level function if it is supposed to
ignore day saving time while transforming the timestamps. This can be a
desired feature for time series strictly following the position of the sun
such as hydrological time series. Doing so ensures that diurnal variations
are preserved by all means and all intervals are of the “correct”
length, however, a possible limitation might be that the day saving time
shift is invariably assumed to be one hour long. This feature requires that
the periodicity of the time series was recognised and is supported by the
following TALFs of the package:
See Also
Examples
# new DTSg object
x <- DTSg$new(values = flow)
# linear interpolation of missing values
## R6 method
x$colapply(fun = interpolateLinear)$print()
## S3 method
print(colapply(x = x, fun = interpolateLinear))
# daily cumulative sums per month
## R6 method
x$colapply(
fun = cumsum,
helpers = FALSE,
funby = byYm____
)$print()
## S3 method
print(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") >= package_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
)$print()
x$colapply(
fun = wrapper,
f = mean,
k = "5 days",
lag = "-2 days"
)$print()
x$colapply(
fun = runner::runner,
f = mean,
k = "5 days",
lag = "-2 days",
idx = x$getCol(col = ".dateTime")
)$print()
x$colapply(
fun = runner::runner,
f = mean,
k = "5 days",
lag = "-2 days",
idx = x[".dateTime"]
)$print()
## S3 method
print(colapply(
x = x,
fun = runner::runner,
f = mean,
k = 5,
lag = -2
))
print(colapply(
x = x,
fun = wrapper,
f = mean,
k = "5 days",
lag = "-2 days"
))
print(colapply(
x = x,
fun = runner::runner,
f = mean,
k = "5 days",
lag = "-2 days",
idx = getCol(x = x, col = ".dateTime")
))
print(colapply(
x = x,
fun = runner::runner,
f = mean,
k = "5 days",
lag = "-2 days",
idx = x[".dateTime"]
))
}
# calculate rolling correlations somewhat inefficiently with the help of
# 'runner'
if (requireNamespace("runner", quietly = TRUE) &&
packageVersion("runner") >= package_version("0.3.8")) {
wrapper <- function(x, y, f, k, lag, ...) {
runner::runner(
cbind(x, y),
f = function(x) f(x[, 1], x[, 2]),
k = k,
lag = lag
)
}
## R6 method
x$colapply(
fun = wrapper,
y = x["flow"] + rnorm(length(x["flow"])),
f = cor,
k = 5,
lag = -2
)$print()
## S3 method
print(colapply(
x = x,
fun = wrapper,
y = x["flow"] + rnorm(length(x["flow"])),
f = cor,
k = 5,
lag = -2
))
}