Plotting data with varying time-averaging periods
In this recipe, we will learn how we can plot the same time series data by averaging it over different time periods using the aggregate()
function.
Getting ready
We will only use the basic R functions for this recipe. Make sure that you load the openair.csv
dataset.
air<-read.csv("openair.csv")
How to do it...
Let's plot the air pollution time series with weekly and daily averages instead of hourly values:
air$date = as.POSIXct(strptime(air$date, format = "%d/%m/%Y %H:%M", "GMT")) means <- aggregate(air["nox"], format(air["date"],"%Y-%U"),mean, na.rm = TRUE) means$date <- seq(air$date[1], air$date[nrow(air)],length = nrow(means)) plot(means$date, means$nox, type = "l")
means <- aggregate(air["nox"], format(air["date"],"%Y-%j"),mean, ...
Get R: Data Analysis and Visualization now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.