R中不同时间间隔的聚合分钟时间

G点

这里的数据集带有分钟日期时间。

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))

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

分钟时间总计

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

因此,总和有5种可能的输出

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个单独的数据集。

胡安·安东尼奥·罗丹·迪亚兹

尝试这样的事情:

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 

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章