Multiple Moving Medium Columns

Asked

Viewed 46 times

2

I would like to create several columns derived from the column x. These columns should receive moving averages of 2, 5... periods. I tried to do the creation with dplyr::mutate, but it didn’t work.

x <- c(30,35,10,25,15,20,10)

x %>% 
  mutate(spread_3 = rollmean(x, k = 3, fill = NA),
         spread_5 = rollmean(x, k = 5, fill = NA)) %>%
  ungroup()

2 answers

3


I believe the argument align = "right" within the function zoo:rollmean whatever is needed to solve this problem.

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(zoo)
#> 
#> Attaching package: 'zoo'
#> The following objects are masked from 'package:base':
#> 
#>     as.Date, as.Date.numeric

x <- data.frame(x = c(30,35,10,25,15,20,10))

x %>% 
  mutate(spread_3 = rollmean(x, k = 3, fill = NA, align = "right"),
         spread_5 = rollmean(x, k = 5, fill = NA, align = "right"))
#>    x spread_3 spread_5
#> 1 30       NA       NA
#> 2 35       NA       NA
#> 3 10 25.00000       NA
#> 4 25 23.33333       NA
#> 5 15 16.66667       23
#> 6 20 20.00000       21
#> 7 10 15.00000       16

Created on 2021-02-10 by the reprex package (v1.0.0)

  • When I do the simulation this way really right, but when I run on the dataframe, it brings some NA values where I shouldn’t.

  • I open a new question for this item in question or I can deal directly with this already opened item?

  • I would open a new question, following the instructions of that link (mainly in the use of function dput), so that it is possible to reproduce your actual conditions in the best way.

1

The question is tagged dplyr, but here’s an alternative using data table.:

library(data.table)

dt <- as.data.table(x)

dt[, `:=`(spread_3 = frollmean(x, 3), spread_5 = frollmean(x, 5))]

> dt
    x spread_3 spread_5
1: 30       NA       NA
2: 35       NA       NA
3: 10 25.00000       NA
4: 25 23.33333       NA
5: 15 16.66667       23
6: 20 20.00000       21
7: 10 15.00000       16

See the help of data.table::froll for details. The advantage is in case you need to create multiple columns with different windows efficiently:

dt <- as.data.table(x)

for (n in 2:7) set(dt, j = paste0('spread_', n), value = frollmean(x, n))

> dt
    x spread_2 spread_3 spread_4 spread_5 spread_6 spread_7
1: 30       NA       NA       NA       NA       NA       NA
2: 35     32.5       NA       NA       NA       NA       NA
3: 10     22.5 25.00000       NA       NA       NA       NA
4: 25     17.5 23.33333    25.00       NA       NA       NA
5: 15     20.0 16.66667    21.25       23       NA       NA
6: 20     17.5 20.00000    17.50       21 22.50000       NA
7: 10     15.0 15.00000    17.50       16 19.16667 20.71429

Browser other questions tagged

You are not signed in. Login or sign up in order to post.