You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于R语言计算历史逐时气象数据的年逐日逐时均值以构建2024基准序列

实现步骤:基于R语言构建2024年"正常天气"逐时序列

核心思路

通过聚合2015-2023年的逐时气象数据,计算月-日-小时维度的平均值,再将这些平均值映射到2024年的逐时时间轴上,生成代表"正常天气"的序列。


步骤1:数据读取与预处理

假设你的气象数据存储为CSV格式,包含datetime(时间戳)和气象变量列(如temperature、humidity等)。首先加载必要的包并预处理数据:

# 加载所需包
library(tidyverse)
library(lubridate)

# 读取历史数据(替换为你的文件路径)
historical_data <- read_csv("historical_weather_2015-2023.csv")

# 转换时间戳为POSIXct类型,提取月、日、小时维度
historical_data <- historical_data %>%
  mutate(
    datetime = ymd_hms(datetime), # 根据你的时间格式调整,如dmy_hms
    month = month(datetime),
    day = day(datetime),
    hour = hour(datetime)
  )

步骤2:计算逐时平均值

按月-日-小时分组,计算每个气象变量的多年平均值,同时处理缺失值:

# 计算月-日-小时维度的平均值
hourly_climatology <- historical_data %>%
  group_by(month, day, hour) %>%
  summarise(
    avg_temp = mean(temperature, na.rm = TRUE),
    avg_humidity = mean(humidity, na.rm = TRUE),
    # 添加你需要的其他气象变量
    .groups = "drop"
  )

特殊处理:闰年2月29日

2024年是闰年,若历史数据中2月29日的记录不足(仅2016、2020年有该日期),可以用以下方式处理:

  • 直接使用历史上2月29日的平均值(如果有足够样本)
  • 若数据不足,用2月28日的平均值替代,或对2月28日和3月1日的平均值做插值:
# 示例:如果没有2月29日数据,用2月28日的平均值填充
feb29_avg <- hourly_climatology %>%
  filter(month == 2, day == 28) %>%
  mutate(day = 29)

hourly_climatology <- bind_rows(hourly_climatology, feb29_avg)

步骤3:构建2024年逐时序列

生成2024年完整的逐时时间轴,再将预计算的平均值映射到该时间轴上:

# 生成2024年逐时时间序列
year_2024 <- tibble(
  datetime = seq(
    from = ymd_h("2024-01-01 00"),
    to = ymd_h("2024-12-31 23"),
    by = "hour"
  )
) %>%
  mutate(
    month = month(datetime),
    day = day(datetime),
    hour = hour(datetime)
  )

# 将平均值与2024年时间序列关联
normal_weather_2024 <- year_2024 %>%
  left_join(hourly_climatology, by = c("month", "day", "hour")) %>%
  select(datetime, avg_temp, avg_humidity) # 保留需要的列

验证与输出

检查结果是否完整,无缺失值(根据你的数据情况调整):

# 查看缺失值情况
sum(is.na(normal_weather_2024))

# 输出到CSV
write_csv(normal_weather_2024, "normal_weather_2024.csv")

内容的提问来源于stack exchange,提问作者Iro

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 21:52:43