如何创建包含滚动滚动集中的数据计数的集合

时间:2012-05-24 16:04:34

标签: r xts

我有几个月的流量服务器日志。以下部分示例

"UploadDateGMT","UserFileSize","TotalBusinessUnits"
"2012-01-01 00:00:38","1223","1"
"2012-01-01 00:01:16","1302","1"
"2012-01-01 00:08:10","1302","1"

我想将其转换为一个数据集,其中我有一个滚动基础上每五分钟窗口中提交的提交字节数的计数。 (即0-5,1-6,2-7等)从这里,我可以提取最大载荷,95%载荷,制作漂亮的载荷图等。

2 个答案:

答案 0 :(得分:3)

扩展@ PLapointe的answer

endp <- endpoints(tab2, on="mins", k=1) # 1 minute endpoints
onemin <- period.apply(tab2,endp,sum)   # sum per 1-minute period
onemin <- align.time(onemin)            # align to end-of-period times
# all one-minute increments from start--end of onemin
allonemin <- seq(start(onemin), end(onemin), by="1 min")
onemin <- merge(onemin, xts(,allonemin))
fivemin <-  rollapplyr(onemin, 5, sum, na.rm=TRUE, fill=NA)

答案 1 :(得分:2)

xts包可以解决这个问题:

library(xts)
tab <-read.table(text="UploadDateGMT,UserFileSize,TotalBusinessUnits
'2012-01-01 00:00:38',1223,1
'2012-01-01 00:01:16',1302,1
'2012-01-01 00:08:10',1302,1", header=TRUE, as.is=TRUE,sep = ",")

tab2<-xts(tab$UserFileSize,order.by=as.POSIXct(tab$UploadDateGMT) ) #create xts object
endp <-endpoints(tab2, on="mins", k=5) #5 minutes endpoints
fivemin <-period.apply(tab2,endp,sum) #sum per 5-minute period
fivemin

                    [,1]
2012-01-01 00:01:16 2525
2012-01-01 00:08:10 1302

如果您希望时间列以5分钟为增量:

res<- align.time( fivemin[endpoints(fivemin, on="mins", k=5)], n=60*5)