【问题标题】:How to create a sequence of intervals for allocating a value如何创建用于分配值的间隔序列
【发布时间】:2012-05-08 00:14:31
【问题描述】:

我正在尝试找到将会话开始和结束时间之间的秒数分成 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 可能是最好的方法。

【问题讨论】:

  • 你能解释一下你的“时钟”从哪里开始吗?我看不出start 时间和01:00 中的值之间有任何关系
  • 没有关系,这只是所需输出的示例。我会回去编辑它,以便示例输出对齐。谢谢
  • 更正以排列输出示例。
  • 对迟到的更改和匆忙的格式表示歉意

标签: r session time intervals xts


【解决方案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)

【讨论】:

  • 这正是我一直在寻找的,非常感谢 joshua ...我所做的唯一更改是将 xa 除以 900 秒以获得间隔中的比例比特率并转换 xa 和 cbind回到 df ...我还没有看到它是如何扩展到几百万行的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-02
  • 1970-01-01
  • 1970-01-01
  • 2014-07-20
  • 2022-12-28
  • 1970-01-01
相关资源
最近更新 更多