gpt4 book ai didi

r - 在 data.table v1.8.10 vs v1.9.2 中对非常小的数字(例如 1e-28)和 0.0 进行分组

转载 作者:行者123 更新时间:2023-12-05 01:04:54 24 4
gpt4 key购买 nike

我注意到 R 中 data.table 创建的频率表似乎无法区分非常小的数字和零?我可以改变这种行为还是这是一个错误?

可重现的例子:

>library(data.table)   
DT <- data.table(c(0.0000000000000000000000000001,2,9999,0))
test1 <- as.data.frame(unique(DT[,V1]))
test2 <- DT[, .N, by = V1]

如您所见,频率表 (test2) 不会识别 0.0000000000000000000000000001 和 0 之间的差异,并将两个观察值放在同一类中。

数据表版本:1.8.10
右:3.02

最佳答案

值得一读R FAQ 7.31并考虑浮点表示的准确性。

我无法在当前的 cran 版本 (1.9.2) 中重现这一点。使用

R version 3.0.3 (2014-03-06)
Platform: x86_64-w64-mingw32/x64 (64-bit)

我猜测行为的变化将与这个新闻项目有关。

o Numeric data is still joined and grouped within tolerance as before but instead of tolerance being sqrt(.Machine$double.eps) == 1.490116e-08 (the same as base::all.equal's default) the significand is now rounded to the last 2 bytes, apx 11 s.f. This is more appropriate for large (1.23e20) and small (1.23e-20) numerics and is faster via a simple bit twiddle. A few functions provided a 'tolerance' argument but this wasn't being passed through so has been removed. We aim to add a global option (e.g. 2, 1 or 0 byte rounding) in a future release.



来自马特的更新

是的,这是 v1.9.2 和 data.table 中的故意更改现在区分 0.0000000000000000000000000001来自 0 (正如 user3340145 正确认为应该的那样)由于改进的舍入方法来自 NEWS .

我还添加了 for从 Rick 的回答到测试套件的循环测试。

顺便说一句,#5369 现在已在 v1.9.3 中实现(尽管此问题不需要这些):

o bit64::integer64 now works in grouping and joins, #5369. Thanks to James Sams for highlighting UPCs.

o New function setNumericRounding() may be used to reduce to 1 byte or 0 byte rounding when joining to or grouping columns of type 'numeric', #5369. See example in ?setNumericRounding and NEWS item from v1.9.2. getNumericRounding() returns the current setting.



请注意,舍入现在(从 v1.9.2 开始)与有效数的准确性有关;即有效数字的数量。 0.0000000000000000000000000001 == 1.0e-28精确到 1 s.f.,因此新的舍入方法不会将其与 0.0 组合在一起.

简而言之,问题的答案是:从 v1.8.10 升级到 v1.9.2 或更高版本。

关于r - 在 data.table v1.8.10 vs v1.9.2 中对非常小的数字(例如 1e-28)和 0.0 进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22290544/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com