Moving Average of Previous Three Values in R

Moving average of previous three values in R

A simplier implementation of w_i_l_l's mavback function based on his mav function

mavback <- function(x,n){ filter(x, c(0, rep(1/n,n)), sides=1) }

How to calculate moving average from previous rows in data.table?

You could shift the values before applying frollmean with na.rm = TRUE argument:

df[order(store,week),desired:=frollmean(shift(demand),n=2,na.rm=T),by=.(store)][]

store week demand desired
<char> <int> <num> <num>
1: A 3 19.18 NA
2: A 5 NA 19.180
3: A 6 NA 19.180
4: A 8 19.55 NaN
5: A 9 20.50 19.550
6: A 10 NA 20.025
7: B 1 20.75 NA
8: B 2 17.70 20.750
9: B 4 19.40 19.225
10: B 7 17.52 18.550

Calculating moving average

  • Rolling Means/Maximums/Medians in the zoo package (rollmean)
  • MovingAverages in TTR
  • ma in forecast

Take average of last n values and move a value down at a time

Using zoo and dplyr:

library(dplyr)
library(zoo)

df <- df %>% mutate(moving_average = round(rollmeanr(col1, 7, fill = NA)))


> df
col1 moving_average
1 96 NA
2 56 NA
3 53 NA
4 63 NA
5 70 NA
6 65 NA
7 72 68
8 111 70
9 65 71
10 58 72
11 59 71
12 74 72
13 57 71
14 70 71
15 89 67
16 60 67
17 60 67
18 52 66
19 65 65
20 58 65
21 84 67
22 54 62
23 55 61
24 51 60
25 72 63

How to calculate a moving average in R

This is a base R solution.

First get the dates as class date, then store the order of the months, define a range (span_month) and finally move over all windows.

Month_t <- as.Date( paste0(
substr(Month,1,4),"-",substr(Month,5,length(Month)),"-1"),
"%Y-%m-%d" )

Month_ord <- order(Month_t)

span_month=12

sapply( 1:((length(Month_ord) + 1) - span_month),
function(x) mean(emp[Month_ord[x]:Month_ord[x + (span_month - 1)]]) )
# [1] 13.00000 15.00000 17.00000 19.00000 21.00000 23.16667 25.16667 27.16667
# [9] 29.16667 31.16667 33.16667 35.16667 37.16667 39.00000 40.83333 42.66667
#[17] 44.33333 45.83333 47.50000

calculate moving average across columns in R

Assuming that the question intended c((9+13+8)/3, (14+2+10)/3, (16+7+1)/3)
as the values for 1962 and not the values shown there, rollmean can be used in either of the following manners. These one-liners give matrices as the result but as.data.frame can be used on the result if it is important that it be a data frame.

library(zoo)

t(apply(DF, 1, rollmean, 3))
## 1962 1963 1964 1965 1966
## 1 10.0000 8.3333 9.0000 6.6667 11.667
## 2 8.6667 6.0000 10.3333 9.3333 13.000
## 3 8.0000 8.6667 7.3333 10.0000 6.000

t(rollmean(t(DF), 3))
## [,1] [,2] [,3] [,4] [,5]
## 1 10.0000 8.3333 9.0000 6.6667 11.667
## 2 8.6667 6.0000 10.3333 9.3333 13.000
## 3 8.0000 8.6667 7.3333 10.0000 6.000

Note

The input in reproducible form:

Lines <- "
1961 1962 1963 1964 1965 1966 1967
1 9 13 8 4 15 1 19
2 14 2 10 6 15 7 17
3 16 7 1 18 3 9 6"
DF <- read.table(text = Lines, check.names = FALSE)

Using rollmean to calculate a moving average excluding the first observation in R

I found the simplest way to work this was with the lag function.

data$turnout <- lag(rollmean(data[,2], 1, fill = NA),1)

R - How to make a mean/average of n previous values, excluding current observation (rolling average)

We need to take the lag of the output derived from rollapply.

library(dplyr)
library(zoo)
LateCounts %>%
mutate(RollAvge= lag(rollapplyr(Count, 12, mean, partial = TRUE)))

-output

#      Date Count RollAvge
#1 Jan-19 7 NA
#2 Feb-19 4 7.000000
#3 Mar-19 9 5.500000
#4 Apr-19 8 6.666667
#5 May-19 7 7.000000
#6 Jun-19 4 7.000000
#7 Jul-19 4 6.500000
#8 Aug-19 5 6.142857
#9 Sep-19 2 6.000000
#10 Oct-19 5 5.555556
#11 Nov-19 7 5.500000
#12 Dec-19 4 5.636364
#13 Jan-20 3 5.500000
#14 Feb-20 4 5.166667
#15 Mar-20 5 5.166667
#16 Apr-20 2 4.833333
#17 May-20 3 4.333333
#18 Jun-20 2 4.000000
#19 Jul-20 3 3.833333
#20 Aug-20 4 3.750000
#21 Sep-20 3 3.666667
#22 Oct-20 2 3.750000

data

LateCounts <- structure(list(Date = c("Jan-19", "Feb-19", "Mar-19", "Apr-19", 
"May-19", "Jun-19", "Jul-19", "Aug-19", "Sep-19", "Oct-19", "Nov-19",
"Dec-19", "Jan-20", "Feb-20", "Mar-20", "Apr-20", "May-20", "Jun-20",
"Jul-20", "Aug-20", "Sep-20", "Oct-20"), Count = c(7L, 4L, 9L,
8L, 7L, 4L, 4L, 5L, 2L, 5L, 7L, 4L, 3L, 4L, 5L, 2L, 3L, 2L, 3L,
4L, 3L, 2L)), class = "data.frame", row.names = c("1", "2", "3",
"4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15",
"16", "17", "18", "19", "20", "21", "22"))


Related Topics



Leave a reply



Submit