zoo_aggregate function

Aggregate Cases in Zoo Time Series

Aggregate Cases in Zoo Time Series

zoo_aggregate(x = NULL, new_time = NULL, f = mean, ...)

Arguments

  • x: (required, zoo object) Time series to aggregate. Default: NULL

  • new_time: (optional, zoo object, keyword, or time vector) New time to aggregate x to. The available options are:

    • NULL: the highest resolution keyword returned by zoo_time(x)$keywords is used to generate a new time vector to aggregate x.
    • zoo object: the index of the given zoo object is used as template to aggregate x.
    • time vector: a vector with new times to resample x to. If time in x is of class "numeric", this vector must be numeric as well. Otherwise, vectors of classes "Date" and "POSIXct" can be used indistinctly.
    • keyword: a valid keyword returned by zoo_time(x)$keywords, used to generate a time vector with the relevant units.
    • numeric of length 1: interpreted as new time interval, in the highest resolution units returned by zoo_time(x)$units.
  • f: (optional, quoted or unquoted function name) Name of a standard or custom function to aggregate numeric vectors. Typical examples are mean, max,min, median, and quantile. Default: mean.

  • ...: (optional, additional arguments) additional arguments to f.

Returns

zoo object

Examples

#full range of calendar dates x <- zoo_simulate( rows = 1000, time_range = c( "0000-01-01", as.character(Sys.Date()) ) ) #plot time series if(interactive()){ zoo_plot(x) } #find valid aggregation keywords x_time <- zoo_time(x) x_time$keywords #mean value by millennia (extreme case!!!) x_millennia <- zoo_aggregate( x = x, new_time = "millennia", f = mean ) if(interactive()){ zoo_plot(x_millennia) } #max value by centuries x_centuries <- zoo_aggregate( x = x, new_time = "centuries", f = max ) if(interactive()){ zoo_plot(x_centuries) } #quantile 0.75 value by centuries x_centuries <- zoo_aggregate( x = x, new_time = "centuries", f = stats::quantile, probs = 0.75 #argument of stats::quantile() ) if(interactive()){ zoo_plot(x_centuries) }

See Also

Other zoo_functions: zoo_name_clean(), zoo_name_get(), zoo_name_set(), zoo_permute(), zoo_plot(), zoo_resample(), zoo_smooth_exponential(), zoo_smooth_window(), zoo_time(), zoo_to_tsl(), zoo_vector_to_matrix()

  • Maintainer: Blas M. Benito
  • License: MIT + file LICENSE
  • Last published: 2025-02-01