我正在尝试找到一种最有效的方法,将会话开始和结束时间之间的秒数分成15分钟,我可以显示每个时间间隔内的秒数和比特率。
以下是一些示例数据:
df <- structure(list(username = structure(c(1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L, 9L, 9L), .Label = c("user1", "user2", "user3",
"user4", "user5", "user6", "user7", "user8",
"user9"), class = "factor"), bitrate = structure(c(3500000, 7000000,
3500000, 3500000, 3500000, 7000000, 3500000, 7000000, 3500000, 7000000), class = "numeric"),
start = structure(c(1322700567, 1322700984, 1322700646, 1322700883,
1322700042, 1322700073, 1322700547, 1322700794, 1322700694,
1322700934), tzone = "", class = c("POSIXct", "POSIXt")),
end = structure(c(1322700766, 1322701250, 1322700945, 1322701270,
1322701284, 1322706303, 1322701781, 1322702307, 1322701600,
1322701224), tzone = "", class = c("POSIXct", "POSIXt"))), .Names = c("username",
"birate", "start", "end"), row.names = c(NA, 10L), class = "data.frame")
username birate start end
1 user1 3500000 2011-12-01 01:49:27 2011-12-01 01:52:46
2 user2 7000000 2011-12-01 01:56:24 2011-12-01 02:00:50
3 user3 3500000 2011-12-01 01:50:46 2011-12-01 01:55:45
4 user4 3500000 2011-12-01 01:54:43 2011-12-01 02:01:10
5 user5 3500000 2011-12-01 01:40:42 2011-12-01 02:01:24
6 user6 7000000 2011-12-01 01:41:13 2011-12-01 03:25:03
7 user7 3500000 2011-12-01 01:49:07 2011-12-01 02:09:41
8 user8 7000000 2011-12-01 01:53:14 2011-12-01 02:18:27
9 user9 3500000 2011-12-01 01:51:34 2011-12-01 02:06:40
10 user9 7000000 2011-12-01 01:55:34 2011-12-01 02:00:24
&GT;
理想情况下,如果可能的话,我想在R中执行此操作,只需要在1个日历日内,在向量中显示秒数或将比特率向量分配为秒的倍数,例如使用秒: / p>
session 01:30 01:45 02:00 02:15 02:30 etc.
1 0 199 0 0 0 etc.
2 0 266 0 0 0 etc.
10 0 306 24 0 0 etc.
我正在考虑按分钟顺序或者使用具有对齐时间的xts可能是最好的方法。
答案 0 :(得分:1)
我不确定这段代码是你想要做的,但我希望它能帮助你朝着理想的方向前进。
fun <- function(i, d) {
idx <- seq(d$start[i],d$end[i],1) # create sequence for index
dat <- rep(d$birate[i],length(idx)) # create data over sequence
xts(dat, idx, dimnames=list(NULL,d$username[i])) # xts object
}
# loop over each row and put each row into its own xts object
xl <- lapply(1:NROW(df), fun, d=df)
# merge all the xts objects
xx <- do.call(merge, xl)
# apply a function (e.g. colMeans) to each 15-minute period
xa <- period.apply(xx, endpoints(xx, 'minutes', 15), colMeans, na.rm=TRUE)