计算 R 中固定日期范围内多年数据的平均值

阿姆利特

我有40多年的每日温度数据。这是示例数据:

       date tmax
1   1971-01-01 18.9
2   1971-01-02 19.0
3   1971-01-03 19.5
4   1971-01-04 19.2
5   1971-01-05 19.5
.
.
.
17536   2020-12-29 19.7
17537   2020-12-30 18.9

我想计算作物生长期间的温度平均值,即每年 6 月 7 日至 11 月 9 日。我们如何在 r 中做到这一点?

罗纳克·沙阿

这是一个选项——

从数据中获取日期、月份和年份,filter并仅保留 6 月 7 日至 11 月 9 日的行并获取tmax每个year.

library(dplyr)
library(lubridate)

df %>%
  mutate(date = as.Date(date), 
         day = day(date), 
         month = month(date), 
         year = year(date)) %>%
  filter(between(month, 7, 10) | 
         day >= 7  & month == 6 | 
         day <= 9 & month == 11) %>%
  group_by(year) %>%
  summarise(tmax = mean(tmax, na.rm = TRUE))

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章