0

我正在尝试找到将会话开始和结束时间之间的秒数分成 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

>

理想情况下,如果可能,我想在 R 中执行此操作,只需要 1 个日历日,要么在向量中显示秒数,要么将比特率向量分配为秒的倍数,例如秒:

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 可能是最好的方法。

4

1 回答 1

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)
于 2012-04-27T14:23:59.767 回答