R/aggregate.R

Defines functions dtt_aggregate.hms dtt_aggregate.POSIXct dtt_aggregate.Date dtt_aggregate

Documented in dtt_aggregate dtt_aggregate.Date dtt_aggregate.hms dtt_aggregate.POSIXct

#' Aggregates
#'
#' Aggregates a date/time vector
#'
#' The possible units values are 'seconds', 'minutes', 'hours', 'days', 'months'
#'  or 'years'.
#'
#' @param x A date/time vector.
#' @param units A string of the units to aggregate by.
#' @param ... Unused.
#' @return The floored date/time vector.
#' @export
#'
#' @examples
#' dtt_aggregate(as.Date(c("1992-01-01", "1991-02-02", "1991-03-03")), "years")
dtt_aggregate <- function(x, units, ...) {
  UseMethod("dtt_aggregate")
}

#' @describeIn dtt_aggregate Aggregate a Date vector
#' @export
dtt_aggregate.Date <- function(x, units = "days", ...) {
  chk_unused(...)
  x <- dtt_floor(x, units = units)
  unique(x)
}

#' @describeIn dtt_aggregate Aggregate a POSIXct vector
#' @export
dtt_aggregate.POSIXct <- function(x, units = "seconds", ...) {
  chk_unused(...)
  x <- dtt_floor(x, units = units)
  unique(x)
}

#' @describeIn dtt_aggregate Aggregate a hms vector
#' @export
dtt_aggregate.hms <- function(x, units = "seconds", ...) {
  chk_unused(...)
  x <- dtt_floor(x, units = units)
  x <- unique(x)
  dtt_time(x)
}

Try the dttr2 package in your browser

Any scripts or data that you put into this service are public.

dttr2 documentation built on July 10, 2020, 5:06 p.m.