openair (version 0.5-2)

timeAverage: Function to calculate timeAverages for data frames

Description

Function to flexibly aggregate or expand data frames by different time periods, calculating vector-averaged wind direction where appropriate. The averaged periods can also take account of data capture rates.

Usage

timeAverage(mydata,
avg.time = "day",
data.thresh = 0,
statistic = "mean",
percentile = NA,
start.date = NA)

Arguments

mydata
A data frame containing a date field . Can be class POSIXct or Date.
avg.time
This defines the time period to average to. Can be "sec", "min", "hour", "day", "DSTday", "week", "month", "quarter" or "year". For much increased flexibility a number can precede these options followed by a space. For example, a time average of
data.thresh
The data capture threshold to use (%). A value of zero means that all available data will be used in a particular period regardless if of the number of values available. Conversely, a value of 100 will mean that all data will need to be present f
statistic
The statistic to apply when aggregating the data; default is the mean. Can be one of "mean", "max", "min", "median", "sum", "frequency", "sd", "percentile". Note that "sd" is the standard deviation and "frequency" is the number (frequency) of val
percentile
The percentile level in % used when statistic = "percentile". The default is 95.
start.date
A string giving a start date to use. This is sometimes useful if a time series starts between obvious intervals. For example, for a 1-minute time series that starts "2009-11-29 12:07:00" that needs to be averaged up to 15-minute means, the intervals wo

Value

  • Returns a data frame with date in class POSIXct and will remove any non-numeric columns except a column "site".

Warning

timeAverage will remove any character or factor columns - except if a column "site" is available. This is necessary because characters/factors cannot be sensibly averaged. If merging with a daily time series with dates in Date format, it will be necessary to convert the format of the output e.g. mydata$date <- as.Date(mydata$date) before merging. Also note that due to the vector averging of wind direction, any data with ws = 0 and a non-zero wind direction will yield a result with zero wind direction e.g. due to u * sin(2 * pi * theta / 360). In essence the function assumes there cannot be a valid wind direction when the wind speed is zero.

Details

This function calculates time averages for a data frame. It also treats wind direction correctly through vector-averaging. For example, the average of 350 degrees and 10 degrees is either 0 or 360 - not 180. The calculations therefore average the wind components. timeAverage should be useful in many circumstances where it is necessary to work with different time average data. For example, hourly air pollution data and 15-minute meteorological data. To merge the two data sets timeAverage can be used to make the meteorological data 1-hour means first. Alternatively, timeAverage can be used to expand the hourly data to 15 minute data - see example below. For the research community timeAverage should be useful for dealing with outputs from instruments where there are a range of time periods used. It is also very useful for plotting data using timePlot. Often the data are too dense to see patterns and setting different averaging periods easily helps with interpretation.

See Also

See timePlot that plots time series data and uses timeAverage to aggregate data where necessary.

Examples

Run this code
## daily average values
daily <- timeAverage(mydata, avg.time = "day")

## daily average values ensuring at least 75 \% data capture
## i.e. at least 18 valid hours
daily <- timeAverage(mydata, avg.time = "day", data.thresh = 75)

## 2-weekly averages
fortnight <- timeAverage(mydata, avg.time = "2 week")

## make a 15-minute time series from an hourly one
min15 <-  timeAverage(mydata, avg.time = "15 min")

Run the code above in your browser using DataLab