r - 在 5 分钟内计算出现次数的更快方法?

标签 r

我有一个矩阵,events ,其中包含 500 万个事件的发生时间。这 500 万个事件中的每一个都有一个“类型”,范围从 1 到 2000。矩阵的一个非常简化的版本如下所示。 “时间”的单位是自 1970 年以来的秒。所有事件都发生在 2012 年 1 月 1 日之后。

>events
      type          times
      1           1352861760
      1           1362377700
      2           1365491820
      2           1368216180
      2           1362088800
      2           1362377700

我试图将自 2012 年 1 月 1 日以来的时间划分为 5 分钟的存储桶,然后用 i 类型的每个事件的数量填充每个存储桶每个桶都发生过。我的代码如下。请注意 types是一个向量,包含从 1 到 2000 的所有可能类型,by设置为 300,因为这是 5 分钟内的秒数。
for(i in 1:length(types)){
    local <- events[events$type==types[i],c("type", "times")]
    assign(sprintf("a%d", i),table(cut(local$times, breaks=seq(range(events$times)[1],range(events$times)[2], by=300))))
}

这导致变量 a1通过a2000其中包含类型 i 出现次数的行向量每个 5 分钟的桶中都有。

然后我继续找到“a1”和“a2000”之间的所有成对相关性。

有没有办法优化我上面提供的代码块?它运行得很慢,但我想不出办法让它更快。也许桶太多,时间太少。

任何见解将不胜感激。

可重现的示例:
>head(events)
     type         times
      12           1308575460
      12           1308676680
      12           1308825420
      12           1309152660
      12           1309879140
      25           1309946460

xevents <- xts(events[,"type"],.POSIXct(events[,"times"]))
ep <- endpoints(xevents, "minutes", 5)
counts <- period.apply(xevents, ep, tabulate, nbins=length(types))

>head(counts)
                       1    2    3    4    5   6    7    8    9   10   11  12   13   14
2011-06-20 09:11:00    0    0    0    0    0   0    0    0    0    0    0   1    0   0
2011-06-21 13:18:00    0    0    0    0    0   0    0    0    0    0    0   1    0   0
2011-06-23 06:37:00    0    0    0    0    0   0    0    0    0    0    0   1    0   0
2011-06-27 01:31:00    0    0    0    0    0   0    0    0    0    0    0   1    0   0
2011-07-05 11:19:00    0    0    0    0    0   0    0    0    0    0    0   1    0   0
2011-07-06 06:01:00    0    0    0    0    0   0    0    0    0    0    0   0    0   0

>> ep[1:20]
[1]  0  1  2  3  4  5  6  7  8  9 10 12 20 21 22 23 24 25 26 27

上面是我一直在使用的代码,但问题是它没有增加 5 分钟:它只是随着实际事件的发生而增加。

最佳答案

我会为此使用 xts 包。使用 period.apply 在不重叠的 5 分钟间隔内运行函数很容易和 endpoints职能。

# create sample data
library(xts)
set.seed(21)
N <- 1e6
events <- cbind(sample(2000, N, replace=TRUE),
  as.POSIXct("2012-01-01")+sample(1e7,N))
colnames(events) <- c("type","times")
# create xts object
xevents <- xts(events[,"type"], .POSIXct(events[,"times"]))
# find the last row of each non-overlapping 5-minute interval
ep <- endpoints(xevents, "minutes", 5)
# count the number of occurrences of each "type"
counts <- period.apply(xevents, ep, tabulate, nbins=2000)
# set colnames
colnames(counts) <- paste0("a",1:ncol(counts))
# calculate correlation
#cc <- cor(counts)

更新以回应 OP 的评论/编辑:
# Create a sequence of 5-minute steps, from the actual start of the data
m5 <- seq(round(start(xevents),'mins'), end(xevents), by='5 mins')
# Create a sequence of 5-minute steps, from the start of 2012-01-01
m5 <- seq(as.POSIXct("2012-01-01"), end(xevents), by='5 mins')
# merge xevents with empty 5-minute xts object, and
# subtract 1 second, so endpoints are at end of each 5-minute interval
xevents5 <- merge(xevents, xts(,m5-1))
ep5 <- endpoints(xevents5, "minutes", 5)
counts5 <- period.apply(xevents5, ep5, tabulate, nbins=2000)
colnames(counts5) <- paste0("a",1:ncol(counts5))
# align to the beginning of each 5-minute interval, if you want
counts5 <- align.time(counts5,60*5)

关于r - 在 5 分钟内计算出现次数的更快方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17844918/

相关文章:

r - 在 R 中的数据帧上使用 psych reverse.code()

r - 计算均值的 95% 置信区间

r - 在 R 中,修改类中的值

r - 在查看分类后的混淆矩阵时,R包bartMachine中是否有错误?

R 阿鲁斯 : how to remove certain itemsets from lhs/rhs

r - R中有字典功能吗

R-根据另一列中的 ID 随机化列

r - 如何标记由 ggplot2 组成的分位数-分位数图的点?

php - combn r算法

r - 提取 shapefile 值以指向 R