如何根据 R 中另一列的日期 (month/day/year) 计算列的 yearly/monthly 平均值、最大值、最小值等

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

如何根据 R 中另一列的日期 (month/day/year) 计算一列的 yearly/monthly 平均值、最大值、最小值等。我的日期框架包含每日、每月和从 2013 年 1 月 1 日到 2019 年 12 月 31 日的每小时降水量和流量

  | Date     |precipitation  | Stream A Discharge |  Stream B Discharge | 
----------------------------------------------------------------------------
1  | 1/1/2013 |  0.35        |  2.35              |   3.83              | 
 

例如,我如何计算 R 中 2013 年或 2013 年 1 月或 2014 年 12 月流 A 的 average/mean/max/min 降水量或流量?

将数据更改为 date class,从中提取年份并使用 across 您可以计算多个列的多个统计信息。

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)))