Average of last 12 months

How can we get average of last 12 months for each row in the data?

Thanks for your help!

Hi, a reproducible example would help. What have you tried?

Otherwise, have a look at these:

Calculate the average daily and monthly - tidyverse - Posit Community (rstudio.com)

Summarize Time Series Data by Month or Year Using Tidyverse Pipes in R | Earth Data Science - Earth Lab

m <- matrix(sample(1:12,144,replace = TRUE), ncol = 12, nrow = 12)
colnames(m) <- month.abb
m <- cbind(m[,7:12],m)
l <- (dim(m)[2] - 11):dim(m)[2]
m[,l] |> rowMeans(x = _, na.rm = TRUE)
#>  [1] 5.500000 7.583333 7.500000 5.416667 7.750000 7.000000 6.166667 6.250000
#>  [9] 7.250000 8.083333 5.416667 7.166667

Created on 2023-08-10 with reprex v2.0.2

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.