R中不同时间间隔的聚合分钟时间 [英] aggregation minutes time in different time interval in R

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

问题描述

此处是带有分钟日期时间的数据集.

Here dataset with minutes datetime.

times<-structure(list(date = structure(1:61, .Label = c("13.09.2018 14:00", 
"13.09.2018 14:01", "13.09.2018 14:02", "13.09.2018 14:03", "13.09.2018 14:04", 
"13.09.2018 14:05", "13.09.2018 14:06", "13.09.2018 14:07", "13.09.2018 14:08", 
"13.09.2018 14:09", "13.09.2018 14:10", "13.09.2018 14:11", "13.09.2018 14:12", 
"13.09.2018 14:13", "13.09.2018 14:14", "13.09.2018 14:15", "13.09.2018 14:16", 
"13.09.2018 14:17", "13.09.2018 14:18", "13.09.2018 14:19", "13.09.2018 14:20", 
"13.09.2018 14:21", "13.09.2018 14:22", "13.09.2018 14:23", "13.09.2018 14:24", 
"13.09.2018 14:25", "13.09.2018 14:26", "13.09.2018 14:27", "13.09.2018 14:28", 
"13.09.2018 14:29", "13.09.2018 14:30", "13.09.2018 14:31", "13.09.2018 14:32", 
"13.09.2018 14:33", "13.09.2018 14:34", "13.09.2018 14:35", "13.09.2018 14:36", 
"13.09.2018 14:37", "13.09.2018 14:38", "13.09.2018 14:39", "13.09.2018 14:40", 
"13.09.2018 14:41", "13.09.2018 14:42", "13.09.2018 14:43", "13.09.2018 14:44", 
"13.09.2018 14:45", "13.09.2018 14:46", "13.09.2018 14:47", "13.09.2018 14:48", 
"13.09.2018 14:49", "13.09.2018 14:50", "13.09.2018 14:51", "13.09.2018 14:52", 
"13.09.2018 14:53", "13.09.2018 14:54", "13.09.2018 14:55", "13.09.2018 14:56", 
"13.09.2018 14:57", "13.09.2018 14:58", "13.09.2018 14:59", "13.09.2018 15:00"
), class = "factor"), value = 1:61), .Names = c("date", "value"
), class = "data.frame", row.names = c(NA, -61L))

我希望以任何间隔的变化形式汇总分钟时间(因为我不知道哪个时间间隔对我来说更方便进行预测).

I want aggregate minutes time in any variants of interval (because i don't know with what time interval more convenient for me to perform forecast ).

分钟总数如何计算

1. by 15 minutes
2. by 30 minutes
3. by 45 minutes
4.by 1 hour
5. by day

所以求和5个可能的输出

So 5 possible outputs by sum

15                 varsum
13.09.2018 14:15    136
13.09.2018 14:30    376
13.09.2018 14:45    616
13.09.2018 15:00    810

30  
13.09.2018 14:30    496
13.09.2018 15:00    1426

45  
13.09.2018 14:45    1081
13.09.2018 15:30    856


60  
13.09.2018 15:00    1891

day 1891

它是5个单独的数据集.

It is 5 separately datasets.

推荐答案

尝试如下操作:

library(lubridate)
times$date <- as.POSIXlt(as.character(times$date), format = "%d.%m.%Y %H:%M")
min_cuts <- c(15, 30, 45, 60)
f_cuts <- function(x) {
  minuts <- minutes(times$date)@minute/60
  tt <- as.factor(as.POSIXlt(minutes(x * floor(minuts/x)), origin = "1970-01-01"))
  return(tapply(times$value, tt, sum))
}

out <- sapply(min_cuts, f_cuts)
names(out) <- min_cuts
out
$`15`
2018-09-13 14:00:00 2018-09-13 14:15:00 2018-09-13 14:30:00 
                120                 345                 570
2018-09-13 14:45:00 2018-09-13 15:00:00 
                795                  61 

$`30`
2018-09-13 14:00:00 2018-09-13 14:30:00 2018-09-13 15:00:00 
                465                1365                  61 

$`45`
2018-09-13 13:45:00 2018-09-13 14:30:00 
                465                1426 

$`60`
2018-09-13 14:00:00 2018-09-13 15:00:00 
               1830                  61 

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

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