How to calculate yearly/monthly average, max value, min value etc. of a Column based on the date (month/day/year) on another column in R. My date frame contains daily, monthly and hourly precipitation and discharge for dates starting from Jan 1 ,2013 till December 31, 2019
| Date |precipitation | Stream A Discharge | Stream B Discharge |
----------------------------------------------------------------------------
1 | 1/1/2013 | 0.35 | 2.35 | 3.83 |
For example how would I calculate the average/mean/max/min precipitation or discharge of stream A of the year 2013 or for January 2013, or December 2014 in R?
Change the data to date
class, extract year from it and using across
you can calculate multiple statistics for multiple columns.
library(dplyr)
library(lubridate)
df %>%
mutate(Date = dmy(Date),
year = year(Date),
year_month = format(Date, '%Y-%m')) %>%
group_by(year) %>%
#If you need for every month
#group_by(year_month) %>%
summarise(across(precipitation:Stream.B.Discharge,
list(mean = mean, min = min, max = max)))
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments