r - 在 R 中聚合、重组每小时时间序列数据

标签 r time-series

我在 R 的数据框中有一年的每小时数据:

> str(df.MHwind_load)   # compactly displays structure of data frame
'data.frame':   8760 obs. of  6 variables:
 $ Date         : Factor w/ 365 levels "2010-04-01","2010-04-02",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ Time..HRs.   : int  1 2 3 4 5 6 7 8 9 10 ...
 $ Hour.of.Year : int  1 2 3 4 5 6 7 8 9 10 ...
 $ Wind.MW      : int  375 492 483 476 486 512 421 396 456 453 ...
 $ MSEDCL.Demand: int  13293 13140 12806 12891 13113 13802 14186 14104 14117 14462 ...
 $ Net.Load     : int  12918 12648 12323 12415 12627 13290 13765 13708 13661 14009 ...

在保留每小时结构的同时,我想知道如何提取

  1. 特定月份/月份组
  2. 每月的第一天/第一周等
  3. 一年中的所有星期一、所有星期二等

我尝试使用“cut”但没有结果,在网上查看后认为“lubridate”可能能够做到这一点,但还没有找到合适的例子。我非常感谢有关此问题的帮助。

编辑:数据框中的数据示例如下:

  Date Hour.of.Year  Wind.MW  datetime
1  2010-04-01  1  375  2010-04-01  00:00:00
2  2010-04-01  2  492  2010-04-01  01:00:00
3  2010-04-01  3  483  2010-04-01  02:00:00
4  2010-04-01  4  476  2010-04-01  03:00:00
5  2010-04-01  5  486  2010-04-01  04:00:00
6  2010-04-01  6  512  2010-04-01  05:00:00
7  2010-04-01  7  421  2010-04-01  06:00:00
8  2010-04-01  8  396  2010-04-01  07:00:00
9  2010-04-01  9  456  2010-04-01  08:00:00
10  2010-04-01  10  453  2010-04-01  09:00:00
..  ..  ...  ..........  ........
8758  2011-03-31  8758  302  2011-03-31  21:00:00
8759  2011-03-31  8759  378  2011-03-31  22:00:00
8760  2011-03-31  8760  356  2011-03-31  23:00:00

编辑:我想在同一数据集上执行其他基于时间的操作 1. 对所有数据点进行每小时平均,即一年中每天第一个小时内所有值的平均值。输出将是全年的“每小时概况”(24 个时间点) 2. 每周和每月执行相同的操作,即分别获取 52 和 12 小时的配置文件 3. 进行季节性平均值,例如六月到九月

最佳答案

将日期转换为 lubridate 可以理解的格式,然后分别使用函数 monthmdaywday

假设您有一个 data.frame,其时间存储在 Date 列中,那么您问题的答案将是:

 ###dummy data.frame
 df <- data.frame(Date=c("2012-01-01","2012-02-15","2012-03-01","2012-04-01"),a=1:4) 
 ##1. Select rows for particular month
 subset(df,month(Date)==1)

 ##2a. Select the first day of each month
 subset(df,mday(Date)==1)

 ##2b. Select the first week of each month
 ##get the week numbers which have the first day of the month
 wkd <- subset(week(df$Date),mday(df$Date)==1)
 ##select the weeks with particular numbers
 subset(df,week(Date) %in% wkd)     

 ##3. Select all mondays 
 subset(df,wday(Date)==1)

关于r - 在 R 中聚合、重组每小时时间序列数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9870620/

相关文章:

python - Reticulate - 在不分配给变量的情况下获取 Python 结果

python - 多个时间轴的通用分辨率

r - 在R中处理时间序列的最佳实践是什么?

python - Pandas 中的不规则、不连续的周期

python - 如何使用python中前一周(天)的同一天和时间的值来估算时间序列数据中的缺失值

r - 获取一行中的第一个非 NA 元素

r - 为什么使用 cbind 创建新数据框会自动将数字列转换为因子?

r - 在 PostgreSQL 中使用 r sf::st_write 到非公共(public)模式

javascript - 如何将粒子动画合并到 Shiny 应用程序中

python - Pandas 中的时间序列箱线图