0

我有到达过程的数据,我想将其转换为计数过程。这就是我所做的:

# inter-arrival time in milliseconds
x <- rpareto(100000, location = 10, shape = 1.2)
# arrival time in milliseconds
x.cumsum <- cumsum(x)
# the last arrival
x.max <- max(x.cumsum)
# the time scale for the count data, in this case 1 second
kTimeScale <- 1000

count.length <- ceiling(x.max / kTimeScale)

counts <- rep(0, times = count.length)

for (i in x.cumsum) {
  counts[round(i / kTimeScale)] <- counts[round(i / kTimeScale)] + 1
}

这有效,但对于非常大的数据集(几百万它很慢)。我想知道是否有更好更快的方法来做到这一点?

4

1 回答 1

1

你可以这样做table

countsTable<-table(round(x.cumsum/kTimeScale))
counts[1:10]
##  [1] 24 41  1  2 33 26 20 45 36 19
countsTable[1:10]
## 
##  0  1  2  3  4  5  6  7  8  9 
##  5 24 41  1  2 33 26 20 45 36 

不同之处在于您的函数缺少 0 值。该table函数不会为没有观察值的值输入 0,但您可以执行以下操作来解决此问题:

counts2<-rep(0,length(counts)+1)
counts2[as.integer(names(countsTable))+1]<-countsTable
identical(counts,counts2[-1])    
## [1] TRUE
于 2013-10-17T20:52:20.833 回答