user3689341
user3689341

Reputation: 143

Simple time series analysis with R: aggregating and subsetting

I want to convert monthly data into quarterly averages. These are my 2 datasets:

gas <- UKgas
dd <- UKDriverDeaths

I was able to accomplish (I think) for the dd data as so:

dd.zoo <- zoo(dd)
ddq <- aggregate(dd.zoo, as.yearqtr, mean)

However I cannot figure out how to do this with the gas data...any help?


Follow-up

When I try to subset the data based on date (1969-1984) the resulting data does not include 1969 Q1 and instead includes 1985 Q1...any suggestions on how to fix this? I was just trying to subset as gas[1969:1984].

Upvotes: 0

Views: 295

Answers (1)

Zheyuan Li
Zheyuan Li

Reputation: 73265

Originally I did not plan to post answer, as it looks like you did not pre-check your UKgas dataset to see that it is already a quarterly time series.

But the follow-up question is worth answering. "ts" object comes with many handy generic functions. We can use window to easily subset a time series. To extract the section between first quarter of 1969 and the final quarter of 1984, we can use

window(UKgas, start = c(1969,1), end = c(1984,4))

The result will still be a quarterly time series.

On the other hand, if we use "[" for subsetting, we lose object class:

class(UKgas[1:12])
#[1] "numeric"

Upvotes: 3

Related Questions