[R] changing the time base in a ts
Gabor Grothendieck
ggrothendieck at myway.com
Mon Jan 31 18:51:31 CET 2005
Dr Carbon <drcarbon <at> gmail.com> writes:
:
: I'm probably apporaching this all wrong to start but....
:
: Suppose I have a monthly time series and I want to compute the mean of
: months 6,7, and 8. I want to plot the original time series and the
: seasonal time series, one above the other. When I do that as below the
: time series don't line up for reasons that are obvious. How can I
: change the base of the seasonal time series so I can make my plots
: line up? That is, I want the points for the seasonal plot to line up
: with cycle 6 of the first plot.
:
: Thanks.
:
: dat <- ts(rnorm(12*20), start = c(1980,1), frequency = 12)
: plot(dat)
: dat.sub <- dat
: dat.sub[cycle(dat.sub) < 6] <- NA
: dat.sub[cycle(dat.sub) > 8] <- NA
: dat.sub <- aggregate(dat.sub, nfrequency = 1, FUN = mean, na.rm = T)
: tsp(dat)
: tsp(dat.sub)
: par(mfrow = c(2, 1))
: plot(dat)
: plot(dat.sub, type = "b")
Replace the four lines that begin with dat.sub with this single line:
dat.sub <- ts(filter(dat, c(1,1,1)/3)[cycle(dat)==7], start = 1980.5)
More information about the R-help
mailing list