如何用R的dplyr将15分钟间隔气象数据转为小时级统计数据
使用R语言(dplyr)处理15分钟间隔气象数据:小时级/日级统计
我们有15分钟间隔的气温观测数据,需要用R语言的dplyr语法实现以下统计:
- 小时级气温最小值
- 日级气温最大值
- 日级气温平均值
可复现示例数据
df <- structure(list(date = structure(c(1401104700, 1401105600, 1401106500, 1401107400, 1401108300, 1401109200, 1401110100, 1401111000, 1401111900, 1401112800, 1401113700, 1401114600, 1401115500, 1401116400, 1401117300, 1401118200, 1401119100, 1401120000, 1401120900, 1401121800, 1401122700, 1401123600, 1401124500, 1401125400, 1401126300, 1401127200, 1401128100, 1401129000, 1401129900, 1401130800, 1401131700, 1401132600, 1401133500, 1401134400, 1401135300, 1401136200, 1401137100, 1401138000, 1401138900, 1401139800, 1401140700, 1401141600, 1401142500, 1401143400, 1401144300, 1401145200, 1401146100, 1401147000, 1401147900, 1401148800, 1401149700, 1401150600, 1401151500, 1401152400, 1401153300, 1401154200, 1401155100, 1401156000, 1401156900, 1401157800, 1401158700, 1401159600, 1401160500, 1401161400, 1401162300, 1401163200, 1401164100, 1401165000, 1401165900, 1401166800, 1401167700, 1401168600, 1401169500, 1401170400, 1401171300, 1401172200, 1401173100, 1401174000, 1401174900, 1401175800, 1401176700, 1401177600, 1401178500, 1401179400, 1401180300, 1401181200, 1401182100, 1401183000, 1401183900, 1401184800, 1401185700, 1401186600, 1401187500, 1401188400, 1401189300, 1401190200, 1401191100, 1401192000, 1401192900, 1401193800), tzone = "UTC", class = c("POSIXct", "POSIXt")), temperature = c(25, 25.2, 25.3, 25.1, 25.4, 26, 25.9, 25.6, 26.8, 27.8, 26.8, 26, 26, 26.3, 27, 27, 26.2, 25.8, 24.9, 25.1, 26.3, 25.6, 25.3, 25.2, 25.1, 24.8, 24.7, 24, 23, 22.7, 22.5, 22.5, 22.2, 21.9, 21.5, 21.1, 20.8, 20.5, 20.3, 20.3, 20.2, 20, 19.8, 19.6, 19.2, 19.1, 19.1, 18.9, 18.8, 18.6, 18.3, 18.2, 18.2, 18.2, 18.1, 17.9, 17.8, 17.7, 17.8, 18, 18.1, 18, 18.1, 18.6, 18.7, 18.5, 18.3, 18.1, 18.1, 18.6, 18.8, 18.6, 18.6, 18.3, 18.2, 18, 17.8, 18, 18.2, 18.9, 19.8, 19.6, 19.5, 19.7, 20.2, 21.5, 22.4, 23, 24, 23.3, 23.2, 23.7, 24.5, 24.8, 24.9, 26.3, 25.7, 24.9, 24.9, 26)), row.names = c(NA, -100L), class = c("tbl_df", "tbl", "data.frame")) # 确保date列是POSIXct类型(示例数据已自带,此步骤可选) df$date <- as.POSIXct(df$date)
加载必要工具包
# 首次运行需安装包 install.packages(c("dplyr", "lubridate")) # 加载包 library(dplyr) library(lubridate)
1. 计算小时级气温最小值
利用floor_date()将时间戳向下取整到小时,以此作为分组依据,统计每个小时的气温最小值:
hourly_min <- df %>% group_by(hourly_date = floor_date(date, "hour")) %>% summarise(hourly_min_temp = min(temperature, na.rm = TRUE)) %>% ungroup() # 查看前6行结果 head(hourly_min)
floor_date(date, "hour")会将同一小时内的所有15分钟数据归为一组,比如2014-05-26 11:45:00会被归到11:00的组,12:00:00则归到12:00的组。
2. 计算日级气温最大值与平均值
按自然日期分组,统计每日的气温最大值和平均值:
daily_stats <- df %>% group_by(daily_date = as.Date(date)) %>% summarise(daily_max_temp = max(temperature, na.rm = TRUE), daily_mean_temp = mean(temperature, na.rm = TRUE)) %>% ungroup() # 查看日级统计结果 daily_stats
3. 合并小时级与日级统计结果
如果需要在小时级数据中同时展示当日的最大/平均气温,可以通过两次分组关联实现:
combined_stats <- df %>% # 先计算小时级最小值 group_by(hourly_date = floor_date(date, "hour")) %>% mutate(hourly_min_temp = min(temperature, na.rm = TRUE)) %>% ungroup() %>% # 再计算并关联日级统计 group_by(daily_date = as.Date(date)) %>% mutate(daily_max_temp = max(temperature, na.rm = TRUE), daily_mean_temp = mean(temperature, na.rm = TRUE)) %>% ungroup() %>% # 去重,保留每个小时的唯一统计行 distinct(hourly_date, daily_date, hourly_min_temp, daily_max_temp, daily_mean_temp) # 查看合并结果 head(combined_stats)
内容的提问来源于stack exchange,提问作者Ahsk
相关产品推荐
相关产品推荐

