create data frame with multiple columns that are all rolling averages of different windows

前端 未结 1 1870
闹比i
闹比i 2021-01-15 04:17

I have a \"mydata\" data frame that is 2 columns: a date column and a value column. I would like to append columns to the data frame that are moving average of the \"value\"

相关标签:
1条回答
  • 2021-01-15 04:59

    You could achieve this with the data.table package and the rollmeanr function (or the rollmean function) from the zoo package:

    library(data.table)
    library(zoo)
    # method 1:
    setDT(mydata)[, paste0('MA',2:9) := lapply(2:9, function(x) rollmeanr(value, x, fill = NA))][]
    # method 2:
    setDT(mydata)[, paste0('MA',2:9) := lapply(2:9, rollapplyr, data = value, mean, fill = NA)][]
    

    which both give:

    > mydata
              date value MA2 MA3 MA4 MA5 MA6 MA7 MA8 MA9
     1: 2016-01-01     1  NA  NA  NA  NA  NA  NA  NA  NA
     2: 2016-01-02     2 1.5  NA  NA  NA  NA  NA  NA  NA
     3: 2016-01-03     3 2.5   2  NA  NA  NA  NA  NA  NA
     4: 2016-01-04     4 3.5   3 2.5  NA  NA  NA  NA  NA
     5: 2016-01-05     5 4.5   4 3.5   3  NA  NA  NA  NA
     6: 2016-01-06     6 5.5   5 4.5   4 3.5  NA  NA  NA
     7: 2016-01-07     7 6.5   6 5.5   5 4.5   4  NA  NA
     8: 2016-01-08     8 7.5   7 6.5   6 5.5   5 4.5  NA
     9: 2016-01-09     9 8.5   8 7.5   7 6.5   6 5.5   5
    10: 2016-01-10    10 9.5   9 8.5   8 7.5   7 6.5   6
    

    In response to your comment, you can add the log values by chaining a similar operation:

    setDT(mydata)[, paste0('MA',2:9) := lapply(2:9, function(x) rollmeanr(value, x, fill = NA))
                  ][, paste0('ln',2:9) := lapply(.SD, log), .SDcols = 3:10]
    

    which gives:

    > mydata
              date value MA2 MA3 MA4 MA5 MA6 MA7 MA8 MA9       ln2       ln3       ln4      ln5      ln6      ln7      ln8      ln9
     1: 2016-01-01     1  NA  NA  NA  NA  NA  NA  NA  NA        NA        NA        NA       NA       NA       NA       NA       NA
     2: 2016-01-02     2 1.5  NA  NA  NA  NA  NA  NA  NA 0.4054651        NA        NA       NA       NA       NA       NA       NA
     3: 2016-01-03     3 2.5   2  NA  NA  NA  NA  NA  NA 0.9162907 0.6931472        NA       NA       NA       NA       NA       NA
     4: 2016-01-04     4 3.5   3 2.5  NA  NA  NA  NA  NA 1.2527630 1.0986123 0.9162907       NA       NA       NA       NA       NA
     5: 2016-01-05     5 4.5   4 3.5   3  NA  NA  NA  NA 1.5040774 1.3862944 1.2527630 1.098612       NA       NA       NA       NA
     6: 2016-01-06     6 5.5   5 4.5   4 3.5  NA  NA  NA 1.7047481 1.6094379 1.5040774 1.386294 1.252763       NA       NA       NA
     7: 2016-01-07     7 6.5   6 5.5   5 4.5   4  NA  NA 1.8718022 1.7917595 1.7047481 1.609438 1.504077 1.386294       NA       NA
     8: 2016-01-08     8 7.5   7 6.5   6 5.5   5 4.5  NA 2.0149030 1.9459101 1.8718022 1.791759 1.704748 1.609438 1.504077       NA
     9: 2016-01-09     9 8.5   8 7.5   7 6.5   6 5.5   5 2.1400662 2.0794415 2.0149030 1.945910 1.871802 1.791759 1.704748 1.609438
    10: 2016-01-10    10 9.5   9 8.5   8 7.5   7 6.5   6 2.2512918 2.1972246 2.1400662 2.079442 2.014903 1.945910 1.871802 1.791759
    
    0 讨论(0)
提交回复
热议问题