AggDayHour: Aggregate a time series

View source: R/All.R

AggDayHourR Documentation

Aggregate a time series

Description

Aggregates time series data, creating hourly data from 15 minute data for example.

Usage

AggDayHour(x, func, Freq = "Day", hour = 9)

Arguments

x

a data.frame with POSIXct in the first column and numeric vector in the second.

func

the function used for aggregation; mean, max, or sum, for example.

Freq

Choices are "Day", or "Hour", or a numeric value representing the number of hours for aggregation.

hour

An integer between 0 and 23. This is used if "Day" is chosen in the Freq argument to determine when the day starts.

Details

The function can be used with a data.frame with POSIXct in the first column and a variable in the second. You can choose the level of aggregation in hours, or you can choose daily. In the daily case you can choose which hour of the day to start the aggregation. For example, you might want mean flows from 09:00 rather than midnight. You can also choose the function used to aggregate the data. For example, you might want "sum" for rainfall, and "mean" for flow. When aggregating hourly the aggregation starts at whatever hour is in the first row of x and the associated time stamps will reflect this.

Value

A data.frame with POSIXct in the first column (unless daily is chosen, then it's Date class), and the aggregated variable in the second column

Author(s)

Anthony Hammond

Examples

#Create a data.frame with a normally distributed variable at
#a 15 minute sampling rate.
TS <- seq(as.POSIXct("2000-01-01 00:00:00",
tz = "Europe/London"), as.POSIXct("2001-01-01 00:00:00", tz = "Europe/London"), by = 60*15)
TS <- data.frame(DateTime = TS, Var = rnorm(length(TS), 10, 2))
#use the function to aggregate to an hourly sampling rate, taking the maximum of each hour
Hourly <- AggDayHour(TS, func = max, Freq = "Hour")
#now aggregate with the mean at a daily scale
Daily <- AggDayHour(TS, func = mean, Freq = "Day")
#now aggregate with the sum at a 48 hour scale
Hr48 <- AggDayHour(TS, func = sum, Freq = 48)
#now aggregate with the sum at a 6 hour scale
Hr6 <- AggDayHour(TS, func = sum, Freq = 6)

UKFE documentation built on Nov. 6, 2023, 1:07 a.m.

Related to AggDayHour in UKFE...