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

在R语言中按年份统计连续3天及以上时段的次数、连续天数并获取对应最高气温及日期的技术需求

R语言气象数据连续时段统计解决方案

问题概述

你需要完成三个气象数据处理任务:

  • 按年份统计连续3天及以上时段的出现次数;
  • 统计每个符合条件时段的连续天数;
  • 获取每个时段内temperature_max的最大值及其对应日期。

给定的原始数据代码如下:

date = c("2018-07-26", "2018-07-27", "2018-07-31", "2018-08-03", "2018-08-04", "2018-08-05", "2018-08-06", "2018-08-07", "2019-06-25", "2019-06-26", "2019-06-27", "2019-06-28", "2019-06-29", "2019-06-30", "2019-07-01", "2019-07-05", "2019-07-22", "2019-07-23", "2019-07-24", "2019-07-25", "2019-07-26", "2020-07-21", "2020-07-30", "2020-07-31", "2020-08-01", "2020-08-09", "2020-08-10", "2020-08-11", "2020-08-21")
temperature_min = c(19.8, 21.1, 21.2, 22.0, 24.1, 26.0, 22.2, 25.0, 21.6, 20.4, 20.6, 24.8, 21.3, 24.7, 22.6, 19.6, 22.3, 21.2, 24.8, 24.6, 21.3, 21.5, 20.1, 22.3, 21.7, 19.4, 20.7, 20.6, 24.7)
temperature_max = c(34.7, 36.2, 34.6, 34.9, 36.7, 35.9, 35.8, 35.1, 35.1, 36.6, 38.1, 35.5, 37.6, 36.7, 35.3, 35.2, 34.6, 38.5, 39.7, 39.0, 36.7, 34.5, 37.2, 39.4, 36.4, 37.2, 36.8, 36.4, 35.7)
data = as.data.frame(cbind(date, temperature_min, temperature_max ))

完整解决方案

我推荐使用tidyverse工具链来处理,它在日期操作、分组统计上非常高效。以下是分步实现的代码:

1. 预处理数据

首先要把日期转换为日期格式,温度列转为数值型(因为cbind会把所有列转为字符型),同时提取年份用于后续分组:

# 安装tidyverse(如果还没装)
# install.packages("tidyverse")

library(tidyverse)

data_processed <- data %>%
  mutate(
    date = as.Date(date),
    temperature_min = as.numeric(temperature_min),
    temperature_max = as.numeric(temperature_max),
    year = format(date, "%Y")
  ) %>%
  arrange(date)  # 确保数据按日期排序

2. 识别连续日期组

通过计算相邻日期的差值,把连续的日期归为同一个分组:

data_with_groups <- data_processed %>%
  group_by(year) %>%
  mutate(
    # 计算当前日期与前一天的间隔天数
    date_diff = as.integer(date - lag(date, default = first(date))),
    # 间隔不为1时,标记为新组的开始
    group_id = cumsum(date_diff != 1)
  ) %>%
  ungroup()

3. 筛选并统计目标时段

分组计算每个连续时段的关键指标,然后筛选出连续3天及以上的时段:

result <- data_with_groups %>%
  group_by(year, group_id) %>%
  summarise(
    period_start = min(date),
    period_end = max(date),
    consecutive_days = n(),
    max_temp = max(temperature_max),
    max_temp_date = date[which.max(temperature_max)],
    .groups = "drop"
  ) %>%
  filter(consecutive_days >= 3) %>%
  group_by(year) %>%
  mutate(period_num = row_number()) %>%  # 标记每年内的时段序号
  ungroup()

4. 格式化输出结果

把统计结果转换成自然语言格式,方便阅读:

output_text <- result %>%
  group_by(year) %>%
  summarise(
    text = case_when(
      n() == 1 ~ str_glue("出现1次连续3天及以上的时段({format(period_start, '%Y年%m月%d日')}至{format(period_end, '%Y年%m月%d日')}),连续天数为{consecutive_days}天,该时段内temperature_max的最大值为{max_temp},对应日期为{format(max_temp_date, '%Y年%m月%d日')}"),
      n() > 1 ~ str_c(
        str_glue("出现{n()}次连续3天及以上的时段,"),
        str_c(
          str_glue("第{period_num}次为{format(period_start, '%Y年%m月%d日')}至{format(period_end, '%Y年%m月%d日')},连续天数为{consecutive_days}天,该时段内temperature_max的最大值为{max_temp},对应日期为{format(max_temp_date, '%Y年%m月%d日')}"),
          collapse = ";"
        )
      )
    ),
    .groups = "drop"
  ) %>%
  mutate(text = str_glue("- {year}年:{text}")) %>%
  pull(text)

# 打印最终结果
cat(paste(output_text, collapse = "\n"))

运行结果

执行上述代码后,会输出符合预期的结果:

- 2018年:出现1次连续3天及以上的时段(2018年08月03日至2018年08月07日),连续天数为5天,该时段内temperature_max的最大值为36.7,对应日期为2018年08月04日
- 2019年:出现2次连续3天及以上的时段,第1次为2019年06月25日至2019年07月01日,连续天数为7天,该时段内temperature_max的最大值为38.1,对应日期为2019年06月27日;第2次为2019年07月22日至2019年07月26日,连续天数为5天,该时段内temperature_max的最大值为39.7,对应日期为2019年07月24日
- 2020年:出现2次连续3天及以上的时段,第1次为2020年07月30日至2020年08月01日,连续天数为3天,该时段内temperature_max的最大值为39.4,对应日期为2020年07月31日;第2次为2020年08月09日至2020年08月11日,连续天数为3天,该时段内temperature_max的最大值为37.2,对应日期为2020年08月09日

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 20:07:36