# [R] Take average of previous weeks

Miluji Sb milujisb at gmail.com
Sun Mar 25 16:48:25 CEST 2018

```Dear all,

I have weekly data by city (variable citycode). I would like to take the
average of the previous two, three, four weeks (without the current week)
of the variable called value.

This is what I have tried to compute the average of the two previous weeks;

df = df %>%
mutate(value.lag1 = lag(value, n = 1)) %>%
mutate(value .2.previous = rollapply(data = value.lag1,
width = 2,
FUN = mean,
align = "right",
fill = NA,
na.rm = T))

I crated the lag of the variable first and then attempted to compute the
average but this does not seem to to what I want. What I am doing wrong?
Any help will be appreciated. The data is below. Thank you.

Sincerely,

Milu

structure(list(year = c(1970L, 1970L, 1970L, 1970L, 1970L, 1970L,
1970L, 1970L, 1970L, 1970L), citycode = c(1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L), month = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 3L), week = c(1L, 2L, 3L, 4L, 5L, 5L, 6L, 7L, 8L, 9L), date =
structure(c(1L,
2L, 3L, 4L, 5L, 5L, 6L, 7L, 8L, 9L), .Label = c("1970-01-10",
"1970-01-17", "1970-01-24", "1970-01-31", "1970-02-07", "1970-02-14",
"1970-02-21", "1970-02-28", "1970-03-07"), class = "factor"),
value = c(-15.035, -20.478, -22.245, -23.576, -8.84099999999995,
-18.497, -13.892, -18.974, -15.919, -13.576)), .Names = c("year",
"citycode", "month", "week", "date", "tmin"), row.names = c(NA,
10L), class = "data.frame")

[[alternative HTML version deleted]]

```