Search code examples
rmagrittr

Unable to use frollmean and %>%


I want to pipe my data table to frollmean to calculate rolling average of a column. But I am unable to get it work

head(mergedDT)
        date Operating_hours DRIVING_TIME net_hrs workday
1 2018-03-20             110          759       0    TRUE
2 2018-03-21             121          641      11    TRUE
3 2018-03-22             133          625      12    TRUE
4 2018-03-23             145          672      12    TRUE
5 2018-03-24             145            0       0   FALSE
6 2018-03-25             145            0       0   FALSE
  n_alarms
1        8
2        5
3        4
4        4
5        1
6        1

mergedDT %>% frollmean("n_alarms",2)

Solution

  • You can do:

    mergedDT %>%  mutate(mean=frollmean(n_alarms,2))