R: Very basic example for averaging a Time Series
I am qute new to R and studied several posts and websites about time series and moving averaging but simply cannot find a useful hint averging a special period of time.
My data is a table via readcsv with a date and time in one column and several other columns with values. The time steps in the data are not constant, so sometimes 5 minutes, sometimes 2 hours. Eg.
2014-01-25 14:50:00, 4, 8 2014-01-25 14:55:00, 3, 7 2014-01-25 15:00:00, 1, 4 2014-01-25 15:20:24, 12, 34 2014-01-25 17:19:00, 150, 225 2014-01-25 19:00:00, 300, 400 2014-01-25 21:00:00, NA, NA 2014-01-25 23:19:00, 312, 405
So I look for an averaging plot that
- calculates data average in arbitrary intervals like 30 minutes, 1 hour, 1 day etc. So lower steps should be aggregated and higher steps should be disaggregated.
- (removed, since it is trivial to get value per hour from a time series D which is averaged by X hours with D/x.)
- data flagged as NA should not be taken into account. So the function should not interpolate/smooth through Na gaps and performing a line plot should not connect the points between a NA gap with a line.
I already tried
aggregate(list(value1=data$value1,value2=data$value2), list(time=cut(data$time, "1 hour")), sum)
but this does not fulfill needs 1 and 3 and is not able to disaggregate 2-hourly data steps.
Answering point 3: plot automatically skips NA values and breaks the line.
Try this example:
Now, if you want to 'smooth' or average over time intervals purely for graphical purposes, It's probably easiest to start out by separating your data at each line with an NA and then doing a spline or other smoothing operation on each subsection separately.