聚合分钟到小时需求 [英] Aggregating minutes to hour demand

查看:183
本文介绍了聚合分钟到小时需求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道我是否在这个问题的正确部分,我已经环顾四周,没有找到答案,所以这里是我的问题:

I don't know if I am in the right section for this question, I've looked around and did not find an answer so here is my question:

我有一个CSV文件订购如下:

I have a CSV file ordered as follows:

dat <- read.csv(text="Date,Demand
01/01/2012 00:00:00,5061.5
01/01/2012 00:05:00,5030.0
01/01/2012 00:10:00,5011.5
01/01/2012 00:15:00,4983.5
01/01/2012 00:20:00,4963.4
01/01/2012 00:25:00,4980.6
01/01/2012 00:30:00,4969.4
01/01/2012 00:35:00,4961.7
01/01/2012 00:40:00,4929.0
01/01/2012 00:45:00,4907.1
01/01/2012 00:50:00,4892.8
01/01/2012 00:55:00,4870.1
01/01/2012 01:00:00,4860.4",header=TRUE)

日期格式是,我猜, code>%m-%d-%Y-%H-%M-%S

The date format is, I guess, %m-%d-%Y-%H-%M-%S

我想总结一下要求以获得小时的汇总如下:

I'd like to summarize the demand in order to obtain an aggregation on the hour as follows:

01/01/2012 00:00:00.................59 560.6 MGW/h  
#which is the sum of the 12th first date.    
01/01/2012 01:00:00.................xxxxxxx  MGW/h    
01/01/2012 02:00:00.................xxxxxxx MGW/h    

Of当然我的文件比这更大,我总共有100多万行

Of course my file is way larger than that, I have a total of more than 1 million lines

所以,我希望我让你自己可以理解,也许也有日期格式问题。如果是这样,有人知道如何改变它在好的,我尝试与 as.Date 但结果不是预期的。

So, I hope I made myself understandable enough for you, maybe there is also a date format problem. If so, does someone know how to change it in the good one, I tried with as.Date but the result is not the expected one.

推荐答案

使用示例数据,这样可以工作:

Using the example data, something like this could work:

aggregate(
  list(Demand=dat$Demand),
  list(DateAgg=
   as.POSIXct(trunc(as.POSIXct(dat$Date,format="%m/%d/%Y %H:%M:%S"),"hours"))
  ),
  FUN=sum
)


#              DateAgg  Demand
#1 2012-01-01 00:00:00 59560.6
#2 2012-01-01 01:00:00  4860.4

这篇关于聚合分钟到小时需求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆