Dreamer_y19th
Dreamer_y19th

Reputation: 25

Calculate 7 day average in r

Date rate 7_Day_rate_avg
1967-07-01 12.5 N/a
1967-07-02 12.5 N/a
1967-07-03 6 N/a
1967-07-04 8 N/a
1967-07-05 4 N/a
1967-07-06 2 N/a
1967-07-07 11.5 avg
1967-07-08 12.1 avg
1967-07-09 10 avg
1967-07-10 12.0 avg
1967-07-11 11.1 avg
1967-07-12 10 avg

I'm trying to calculate 7 day rate average using the "rate" column in r using rolling mean function, but I am getting a lot of errors. Not sure where to start. I want the final output to look like 7_Day_rate_avg column

library(zoo) rollmean(rate, date, 7)

Upvotes: 0

Views: 1477

Answers (1)

G. Grothendieck
G. Grothendieck

Reputation: 269461

Assuming data frame dat shown reproducibly in the Note at the end, there are several problems:

  • the library statement must be separated from the next statement by a newline or semicolon
  • rate is a column of a data frame, not an R variable
  • the arguments to rollmean are wrong
  • the alignment was not specified. Since you need right alignment use rollmeanr with an r on the end.
  • fill=NA is needed to specify that you want to fill the first 6 values with NA values.

1) Putting all this together we have:

library(zoo)
transform(dat, avg7 = rollmeanr(rate, 7, fill = NA))

giving this data frame:

         date rate     avg7
1  1967-07-01 12.5       NA
2  1967-07-02 12.5       NA
3  1967-07-03  6.0       NA
4  1967-07-04  8.0       NA
5  1967-07-05  4.0       NA
6  1967-07-06  2.0       NA
7  1967-07-07 11.5 8.071429
8  1967-07-08 12.1 8.014286
9  1967-07-09 10.0 7.657143
10 1967-07-10 12.0 8.514286
11 1967-07-11 11.1 8.957143
12 1967-07-12 10.0 9.814286

2) Alternately convert dat to a zoo object and then cbind it to the rolling mean. In this case we don't need fill= since zoo objects are automatically aligned.

library(zoo)
rate <- read.zoo(dat)
cbind(rate, avg7 = rollmeanr(rate, 7))

giving this zoo object:

           rate     avg7
1967-07-01 12.5       NA
1967-07-02 12.5       NA
1967-07-03  6.0       NA
1967-07-04  8.0       NA
1967-07-05  4.0       NA
1967-07-06  2.0       NA
1967-07-07 11.5 8.071429
1967-07-08 12.1 8.014286
1967-07-09 10.0 7.657143
1967-07-10 12.0 8.514286
1967-07-11 11.1 8.957143
1967-07-12 10.0 9.814286

Note

dat in reproducible form is:

dat <- structure(list(date = c("1967-07-01", "1967-07-02", "1967-07-03", 
"1967-07-04", "1967-07-05", "1967-07-06", "1967-07-07", "1967-07-08", 
"1967-07-09", "1967-07-10", "1967-07-11", "1967-07-12"), rate = c(12.5, 
12.5, 6, 8, 4, 2, 11.5, 12.1, 10, 12, 11.1, 10)), row.names = c(NA, 
-12L), class = "data.frame")

Upvotes: 2

Related Questions