-1

Nevermind, I'm trying to figure out a different method. Thanks so much for the response, much appreciated. How do I close this question or mark as solved?

bigjdawg43
  • 93
  • 1
  • 1
  • 7
  • 1
    Please provide a reproducible example (including a small subset of your data frame, and what you would like the results to look like exactly). It will make it much more possible to help you out. Thanks :)https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – mysteRious Apr 05 '18 at 23:55
  • 1
    You've been given a very generous answer to a very substandard question. Please read the Help page for SO so that future questions are not so completely deficient in code. In particular [MCVE] but also the pages outlining what _not_ to include in questions. – IRTFM Apr 06 '18 at 00:54

1 Answers1

1

You could use purrr::map to calculate averages across varying (rolling) windows. This will allow for a flexible way to specify an arbitrary number of (rolling) windows.

For example:

  1. Let's generate some monthly data for 5 years.

    # Sample data
    set.seed(2018);
    nObs <- 5 * 12;
    dates <- seq(as.Date("2018/01/01"), by = "month", length.out = nObs);
    sample.xts <- xts(x = runif(nObs), order.by = dates);
    
  2. Use purrr::map to calculate the rolling average for different windows using zoo::rollapplyr. Here we calculate rolling averages in windows c(length(sample.xts), 12, 6) which corresponds to averaging across all observations (5 years), a 1 year window and a 6 months window.

    # Calculate mean in three rolling windows: 60 months (all), 12 months, 6 months
    library(tidyverse);
    library(zoo);
    df <- purrr::map(
        c(length(sample.xts), 12, 6),
        ~ data.frame(val = sample.xts) %>%
            rownames_to_column("date") %>%
            mutate(!!paste0("mean_", .x) := rollapplyr(val, .x, mean, by.column = F, fill = NA))) %>%
        reduce(left_join, by = c("date", "val"))
    head(df, n = 13);
    #    date        val mean_60   mean_12    mean_6
    #1  2018-01-01 0.33615347      NA        NA        NA
    #2  2018-02-01 0.46372327      NA        NA        NA
    #3  2018-03-01 0.06058539      NA        NA        NA
    #4  2018-04-01 0.19743361      NA        NA        NA
    #5  2018-05-01 0.47431419      NA        NA        NA
    #6  2018-06-01 0.30104860      NA        NA 0.3055431
    #7  2018-07-01 0.60675886      NA        NA 0.3506440
    #8  2018-08-01 0.13001210      NA        NA 0.2950255
    #9  2018-09-01 0.95865471      NA        NA 0.4447037
    #10 2018-10-01 0.54684949      NA        NA 0.5029397
    #11 2018-11-01 0.39561597      NA        NA 0.4898233
    #12 2018-12-01 0.66453861      NA 0.4279740 0.5504050
    #13 2019-01-01 0.98211229      NA 0.4818039 0.6129639
    

Note that the average based on all observations is the last entry in the data.frame:

tail(df);
#         date       val   mean_60   mean_12    mean_6
#55 2022-07-01 0.6625414        NA 0.4422977 0.4933802
#56 2022-08-01 0.9665480        NA 0.4729062 0.5254261
#57 2022-09-01 0.3932122        NA 0.4555178 0.5234418
#58 2022-10-01 0.6043476        NA 0.5056410 0.5891695
#59 2022-11-01 0.2637507        NA 0.4473775 0.6089818
#60 2022-12-01 0.4894682 0.4809708 0.4824080 0.5633114
Maurits Evers
  • 49,617
  • 4
  • 47
  • 68