How can we get average of last 12 months for each row in the data?
Thanks for your help!
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)
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