Learn R Programming

UKFE (version 0.3.7)

AggDayHour: 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)

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

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".

hour

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

Author

Anthony Hammond

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.

Examples

Run this code
#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")

Run the code above in your browser using DataLab