【问题标题】:Changing quarterly data into hourly data将季度数据更改为每小时数据
【发布时间】:2026-01-18 03:20:05
【问题描述】:

我有如下数据。它是从 01.01.2015~31.12.2015。 数据以季度为基数。但是我想将例如 0:00, 0:15, 0:30, 0:45 加在一起以制作一个小时的数据。如何将其转化为每小时数据?

提前谢谢你。

  Date      Hour Day-ahead Total Load Forecast [MW] - Germany (DE)
01.01.2015  0:00    42955
01.01.2015  0:15    42412
01.01.2015  0:30    41901
01.01.2015  0:45    41355
01.01.2015  1:00    40710
01.01.2015  1:15    40204
01.01.2015  1:30    39640
01.01.2015  1:45    39324
01.01.2015  2:00    39002
01.01.2015  2:15    38869
01.01.2015  2:30    38783
01.01.2015  2:45    38598
01.01.2015  3:00    38626
01.01.2015  3:15    38459
01.01.2015  3:30    38414
...


   > dput(head(new3))
structure(list(Date = structure(c(16436, 16436, 16436, 16436, 
16436, 16436), class = "Date"), Hour = c("0:00", "0:15", "0:30", 
"0:45", "1:00", "1:15"), Dayahead = c("42955", "42412", "41901", 
"41355", "40710", "40204"), Actual = c(42425L, 42021L, 42068L, 
41874L, 41230L, 40810L), Difference = c("530", "391", "-167", 
"-519", "-520", "-606")), .Names = c("Date", "Hour", "Dayahead", 
"Actual", "Difference"), row.names = c(NA, 6L), class = "data.frame")

【问题讨论】:

    标签: r converter aggregate


    【解决方案1】:

    例如,我创建了一个小型数据集。

    df <- read.csv(text = "Date,Hour,Val
    2013-06-03,06:01,0
    2013-06-03,12:08,-1
    2013-06-03,12:48,3.3
    2013-06-03,13:58,2
    2013-06-03,13:01,12
    2013-06-03,13:08,3
    2013-06-03,14:48,4
    2013-06-03,14:58,8
    2013-06-03,15:01,9.2
    2013-06-03,15:08,12.3
    2013-06-03,16:48,0
    2013-06-03,19:58,-10", stringsAsFactors = FALSE)
    

    使用来自dplyrgroup_bysummarize 和来自lubridatefloor_date 可以做到这一点:

    library(dplyr)
    library(lubridate)
    
    df %>%
      group_by(Hours=floor_date(ymd_hm(paste(Date, Hour)), "1 hour")) %>%
      summarize(Val=sum(Val))
    
    # # A tibble: 7 x 2
    #   Hours                  Val
    #   <dttm>               <dbl>
    # 1 2013-03-06 06:00:00   0   
    # 2 2013-03-06 12:00:00   2.30
    # 3 2013-03-06 13:00:00  17.0 
    # 4 2013-03-06 14:00:00  12.0 
    # 5 2013-03-06 15:00:00  21.5 
    # 6 2013-03-06 16:00:00   0   
    # 7 2013-03-06 19:00:00 -10.0 
    

    【讨论】:

    • 您好!我试过这个,但我失败了。我想这是因为我的 Hour 和 Val 数据符合要求。我应该将它们更改为数字来解决吗?
    • @junmouse 它将与字符一起使用,我认为在您的数据集中它们是因子,因此您需要从它们中删除因子,或者使用 stringsAsFactors = FALSE for read.csv 或手动删除。
    • 我有字符值,因为我还添加了 stringsasfactors=F。我粘贴了你的语法,只调整了 Val 部分,它告诉我'# A tibble: 1 x 2 Hours Actual &lt;dttm&gt; &lt;int&gt; 1 NA NA Warning message: All formats failed to parse. No formats found.' 我不知道在这里做什么.. 嗯
    • @junmouse 你能不能dput(head(...))你的数据集到问题中。
    • 我已经把它放在我的帖子上了。我刚刚更新,因为我发现了一些错误。请忽略实际和差异。
    【解决方案2】:

    假设您的数据框称为 df

    > head(df)
            Date        Hour Forecast
    1 01.01.2015 12:00:00 AM    42955
    2 01.01.2015 12:15:00 AM    42412
    3 01.01.2015 12:30:00 AM    41901
    4 01.01.2015 12:45:00 AM    41355
    5 01.01.2015 01:00:00 AM    40710
    6 01.01.2015 01:15:00 AM    40204
    

    您可以通过以下代码将您的预测汇总到每小时

    library(lubridate)
    df$DateTime=paste(df$Date,df$Hour,sep=" ")%>%dmy_hms%>%floor_date(unit="hour")
    result<-ddply(df,.(DateTime),summarize,x=sum(Forecast))
    > result
                 DateTime      x
    1 2015-01-01 00:00:00 168623
    2 2015-01-01 01:00:00 159878
    3 2015-01-01 02:00:00 155252
    4 2015-01-01 03:00:00 115499
    

    变量 x 具有每小时的预测总和。时间戳 00:00:00 聚合时间 00:00、00:15、00:30、00:45。

    【讨论】: