
Sub-hourly or hourly -> n-hourly
subhourly2nhourly.RdGeneric function for aggregating a sub-hourly or hourly time series into a user-defined n-hourly one.
Usage
subhourly2nhourly(x, ...)
# Default S3 method
subhourly2nhourly(x, FUN, n.hours, na.rm=TRUE, na.rm.max=0,
start="00:00:00", start.fmt="%H:%M:%S", tz, ...)
# S3 method for class 'zoo'
subhourly2nhourly(x, FUN, n.hours, na.rm=TRUE, na.rm.max=0,
start="00:00:00", start.fmt="%H:%M:%S", tz, ...)
# S3 method for class 'data.frame'
subhourly2nhourly(x, FUN, n.hours, na.rm=TRUE,
na.rm.max=0, start="00:00:00", start.fmt="%H:%M:%S", tz,
dates=1, date.fmt="%Y-%m-%d %H:%M:%S", out.fmt="zoo",
verbose=TRUE, ...)
# S3 method for class 'matrix'
subhourly2nhourly(x, FUN, n.hours, na.rm=TRUE, na.rm.max=0,
start="00:00:00", start.fmt="%H:%M:%S", tz,
dates=1, date.fmt="%Y-%m-%d %H:%M:%S", out.fmt="zoo",
verbose=TRUE, ...)Arguments
- x
zoo, data.frame or matrix object, with sub-hourly or hourly time series.
Measurements at several gauging stations can be stored in a data.frame or matrix object, and in that case, each column ofxrepresents the time series measured in each gauging station, and the column names ofxshould correspond to the ID of each station.- FUN
Function that have to be applied for aggregating from sub-hourly or hourly into n-hourly time step (e.g., for precipitation
FUN=sumand for temperature and streamflows tsFUN=mean).FUNMUST accept thena.rmargument, becausena.rmis passed toFUN.- n.hours
positive integer indicating the length, in hours, of each output aggregation period.
- na.rm
Logical. Should missing values be removed?
-) TRUE : the n-hourly values are computed only for periods with a percentage of missing values less than or equal tona.rm.max
-) FALSE: if there is AT LEAST one NA within an n-hour period, the corresponding n-hourly value in the output object will beNA.- na.rm.max
Numeric in [0, 1]. It is used to define the maximum percentage of missing values allowed in each n-hour period to keep the aggregated value in the output object of this function. In other words, if the percentage of missing values in a given n-hour period is larger than
na.rm.max, the corresponding n-hourly value will beNA.- start
character, indicating the starting time used for anchoring the n-hour aggregation periods. It MUST be provided in the format specified by
start.fmt.- start.fmt
character indicating the format in which the time is provided in
start, By defaultdate.fmt=%H:%M:%S. Seeformatinas.POSIXct.- tz
character, with the specification of the time zone used in both
xandstart. System-specific (see time zones), but""is the current time zone, and"GMT"is UTC (Universal Time, Coordinated). SeeSys.timezoneandas.POSIXct.
Iftzis missing (the default), it is automatically set to the time zone used intime(x).
Iftzis provided, it forcestime(x)to be in the time zone specified bytz, without modifying the values (hours, minutes, seconds, etc).- dates
numeric, factor, character, POSIXct or POSIXt object indicating how to obtain the dates and times for each column of
x(e.g., gauging station)
Ifdatesis a number, it indicates the index of the column inxthat stores the date and times
Ifdatesis a factor or character, it is converted into POSIXct class, using the date format specified bydate.fmt
Ifdatesis already of POSIXct or POSIXt class, the code verifies that the number of elements on it be equal to the number of elements inx- date.fmt
character indicating the format in which the dates are stored in
dates, By defaultdate.fmt=%Y-%m-%d %H:%M:%S. Seeformatinas.Date.
ONLY required whenclass(dates)=="factor",class(dates)=="character"orclass(dates)=="numeric".- out.fmt
OPTIONAL. Only used when
xis a matrix or data.frame object /cr character, for selecting if the result will be a matrix/data.frame or a zoo object. Valid values are: numeric, zoo (default)- verbose
logical; if TRUE, progress messages are printed
- ...
arguments additional to
na.rmpassed toFUN.
Details
This function assumes that x has an hourly or sub-hourly temporal frequency. The output time index corresponds to the beginning of each n-hour aggregation period.
Author
Mauricio Zambrano-Bigiarini, mzb.devel@gmail
Examples
## Creating a 30-min time sequence and counting its length
dt <- seq(from=as.POSIXct("2021-06-30 00:30", tz="UTC"),
to=as.POSIXct("2021-06-30 23:30", tz="UTC"), by="30 min")
ndt <- length(dt)
## Creating a dummy 30-min zoo object, with 1 as the only value in each time period
x <- zoo(rep(1, ndt), dt)
## Aggregation from 30-minute single ts into 3-hourly ts
h1 <- subhourly2nhourly(x, FUN=sum, n.hours=3, na.rm=TRUE)
## Aggregation of 3 ts with 30-minute time frequency (i.e., a zoo matrix)
## into a 6-hourly zoo object.
X <- cbind(x, x, x)
h2 <- subhourly2nhourly(X, FUN=sum, n.hours=6, na.rm=TRUE)
## Hourly input into 6-hourly output
h <- subhourly2hourly(x, FUN=sum)
h3 <- subhourly2nhourly(h, FUN=sum, n.hours=6, na.rm=TRUE)