Skip to content
Prev 385619 / 398503 Next

monthly mean for each month and each year in the data frame

Hello,

Here are two aggregate options, the first base R only, the second one 
with package zoo.


year_month <- format(df$data_POSIX, "%Y-%m")
aggregate(value ~ year_month, df, mean)    # or even value ~ format(etc)
#  year_month    value
#1    2019-01 6.430922
#2    2019-02 4.846731
#3    2019-03 4.968503
#4    2020-02 6.484031
#5    2020-03 4.910305
#6    2020-04 4.906575


aggregate(value ~ zoo::as.yearmon(data_POSIX), df, mean)
#  zoo::as.yearmon(data_POSIX)    value
#1                    jan 2019 6.430922
#2                    fev 2019 4.846731
#3                    mar 2019 4.968503
#4                    fev 2020 6.484031
#5                    mar 2020 4.910305
#6                    abr 2020 4.906575


Hope this helps,

Rui Barradas

?s 11:19 de 14/09/20, Stefano Sofia escreveu: