r - How to aggregate several months (seasonal) from a "ts" object? -


i'll use airpassengers data set reproducibility:

data(airpassengers) class(aispassengers) ## [1] "ts" airpassengers ##   jan feb mar apr may jun jul aug sep oct nov dec 1949 112 118 132 129 121 135 148 148 136 119 104 118 1950 115 126 141 135 125 149 170 170 158 133 114 140 1951 145 150 178 163 172 178 199 199 184 162 146 166 1952 171 180 193 181 183 218 230 242 209 191 172 194 1953 196 196 236 235 229 243 264 272 237 211 180 201 1954 204 188 235 227 234 264 302 293 259 229 203 229 1955 242 233 267 269 270 315 364 347 312 274 237 278 1956 284 277 317 313 318 374 413 405 355 306 271 306 1957 315 301 356 348 355 422 465 467 404 347 305 336 1958 340 318 362 348 363 435 491 505 404 359 310 337 1959 360 342 406 396 420 472 548 559 463 407 362 405 1960 417 391 419 461 472 535 622 606 508 461 390 432 

is there way obtain annual seasonal average (see expected results table below) without converting "ts" object class?

right i'm able transforming "ts" object xts (package xts) or data frame (then use package seas mkseas).

is there "ts" method use, don't have make transformation/conversion of "ts" object class?

expected results, out of "ts" object:

year average (jun, jul, aug) 1949 143.667 1950 163.00 1951 192.00 ... ... 

1) try aggregate.ts. inputs "ts" object , outputs 1 representing mean of jun, jul , aug of each year. assumes complete years:

aggregate(airpassengers, 1, function(x) mean(x[6:8])) 

giving:

time series: start = 1949  end = 1960  frequency = 1   [1] 143.6667 163.0000 192.0000 230.0000 259.6667 286.3333 342.0000 397.3333  [9] 451.3333 477.0000 526.3333 587.6667 

2) gets increasingly awkward if want allow incomplete first and/or last years here solution. ugly because tapply converting matrix , have manually reconstruct "ts" output series it.

ap2 <- window(airpassengers, start = c(1949, 9)) unname(ts(tapply(ap2, list(floor(time(ap2)), cycle(ap2) %in% 6:8), mean)[, "true"],     start = floor(time(ap2)[1])))  time series: start = 1949  end = 1960  frequency = 1   [1]       na 163.0000 192.0000 230.0000 259.6667 286.3333 342.0000 397.3333  [9] 451.3333 477.0000 526.3333 587.6667 

3) if need situations such (2) lot more straight forward convert zoo (and if need be). if don't need na component in output line marked ## omitted. unlike (2) each operation produces "zoo" series it's cleaner. if wanted "ts" class output use as.ts(zyr2) .

library(zoo) z <- as.zoo(ap2) z678 <- z[cycle(z) %in% 6:8] zyr <- aggregate(z678, floor(time(z678)), mean) zyr2 <- merge(zyr, zoo(, unique(floor(time(z))))) ## 

giving:

> zyr2     1949     1950     1951     1952     1953     1954     1955     1956        na 163.0000 192.0000 230.0000 259.6667 286.3333 342.0000 397.3333      1957     1958     1959     1960  451.3333 477.0000 526.3333 587.6667  

4) readily done using data frames omitting ## line if na row not needed. (this uses base r sqldf, dplyr or data.table alternately used aggregation , subsetting.) if desired convert ts ts(df$ap2, start = df$year[1]) .

df <- data.frame(year = floor(time(ap2)), month = cycle(ap2), ap2 = c(ap2)) ag <- aggregate(ap2 ~ year, subset(df, month %in% 6:8), mean) dfyr <- merge(ag, unique(df["year"]), = true) ## 

giving:

> dfyr    year      ap2 1  1949       na 2  1950 163.0000 3  1951 192.0000 4  1952 230.0000 5  1953 259.6667 6  1954 286.3333 7  1955 342.0000 8  1956 397.3333 9  1957 451.3333 10 1958 477.0000 11 1959 526.3333 12 1960 587.6667 

Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -