3
I have a dataset of information with years on the line. I need to do an average every two years, as the example below, where the first four columns are the existing data and the last two, which is what we want to calculate. I tried with aggregate
and apply
, but I was unsuccessful.
My data is following:
structure(list(
Cod_ARES = c(1, 1, 1, 1, 1, 1, 4, 4, 4, 4, 4, 4),
ano = c(2015, 2016, 2017, 2018, 2019, 2020, 2015, 2016, 2017, 2018, 2019, 2020),
AG001 = c(1, 2, 4, 1, 5, 4, 4, 3, 2, 5, 2, 5),
AG002 = c(3, 2, 2, 4, 5, 1, 5, 1, 1, 1, 2, 3),
AG001M = c(NA, 1.5, 3, 2.5, 3, 4.5, 4, 3.5, 2.5, 3.5, 3.5, 3.5),
AG002M = c(NA, 2.5, 2, 3, 4.5, 3, 3, 3, 1, 1, 1.5, 2.5)),
row.names = c(NA, -12L), class = c("tbl_df", "tbl", "data.frame"))
Marcus, it worked great! I didn’t know the zoo package. Thank you very much! Congratulations on your dedication to those who are learning...
– Daniel Manzi
Thank you! It’s very good to know that my response has helped you in some way. So consider vote and accept the answer, so that in the future other people who experience the same problem have a reference to solve it.
– Marcus Nunes