第一次尝试使用table
and cut
:
table(cut(x, breaks=seq(0,3,length.out=100)))
它避免了额外的输出,但在我的计算机上大约需要 34 秒:
system.time(table(cut(x, breaks=seq(0,3,length.out=100))))
user system elapsed
34.148 0.532 34.696
相比之下,3.5秒hist
:
system.time(hist(x, breaks=seq(0,3,length.out=100), plot=FALSE)$count)
user system elapsed
3.448 0.156 3.605
Using tabulate
and .bincode
运行速度比hist
:
tabulate(.bincode(x, breaks=seq(0,3,length.out=100)), nbins=100)
system.time(tabulate(.bincode(x, breaks=seq(0,3,length.out=100))), nbins=100)
user system elapsed
3.084 0.024 3.107
Using tablulate
and findInterval
相对于table
and cut
相对于hist
:
tabulate(findInterval(x, vec=seq(0,3,length.out=100)), nbins=100)
system.time(tabulate(findInterval(x, vec=seq(0,3,length.out=100))), nbins=100)
user system elapsed
2.044 0.012 2.055