R/aggregate_by_time.R

Defines functions aggregate_by_time

Documented in aggregate_by_time

#' A simple wrapper for aggregate function. Aggregates timeseries to new chosen timestep
#'
#' @param file   a data frame, matrix or array containing one column with POSIXct formated DateTime objects
#' @param columns integer or vector for the columns to be aggregated
#' @param timestep time format of the output. F.e. "1 hour", "5 min", "1 month", "1 weeKk
#' @param FUN    Function applied for aggregation (mean, sum,...) na.rm as default within function
#' @param right = TRUE by default. Takes the values of the hour before for the function. F.e. Precipitation value at 18:00 means sum of values 17:00-18:00
#' when right = TRUE otherwise 18:00 = sum of 18:00-19:00
#' @return returns the file with choosen timly resolution (timestep)
#' @example ## rain_hourly <- aggregate(rain$rain_mm_Tot, list(cut(rain$DateTime, "1 hour")), sum, na.rm = TRUE )
#'
#' @export

aggregate_by_time <- function(file,columns,timestep, FUN, right = TRUE){

  b <- sapply(file, is.POSIXct)
  c <- unname(which(b == TRUE))
  file <- aggregate(file[columns], list(cut(file[[c]], timestep, right = right)), FUN = FUN, na.rm = TRUE )
  return(file)
}
plvoit/PaulsPack documentation built on Dec. 15, 2019, 10:24 p.m.