R - 更快的 hist(XX,plot=FALSE)$count 替代方案

2023-12-03

我正在寻找 R 的更快替代方案hist(x, breaks=XXX, plot=FALSE)$count函数,因为我不需要产生任何其他输出(因为我想在sapply调用,需要调用此函数 100 万次迭代),例如

x = runif(100000000, 2.5, 2.6)
bincounts = hist(x, breaks=seq(0,3,length.out=100), plot=FALSE)$count

有什么想法吗?


第一次尝试使用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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

R - 更快的 hist(XX,plot=FALSE)$count 替代方案 的相关文章

随机推荐