我不知道我是否在正确的部分回答这个问题,我环顾四周并没有找到答案,所以这是我的问题:
我有一个 CSV 文件,订购如下:
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)
我猜日期格式是%m-%d-%Y-%H-%M-%S
我想总结一下需求,以获得按小时的聚合,如下所示:
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
当然,我的文件比这个大得多,总共超过 100 万行
所以,我希望我的表述足够让您理解,也许还存在日期格式问题。如果是这样,有人知道如何将其更改为好的,我尝试使用 as.Date
但结果不是预期的结果。
最佳答案
使用示例数据,类似这样的事情可以工作:
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
关于r - 汇总分钟到小时的需求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22368796/