热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

R的data.table截断位?-R'sdata.tableTruncatingBits?

SoImahugedata.tablefaninR.Iuseitalmostallthetimebuthavecomeacrossasituationin

So I'm a huge data.table fan in R. I use it almost all the time but have come across a situation in which it won't work for me at all. I have a package (internal to my company) that uses R's double to store the value of an unsigned 64 bit integer whose bit sequence corresponds to some fancy encoding. This package works very nicely everywhere except data.table. I found that if I aggregate on a column of this data that I lose a large number of my unique values. My only guess here is that data.table is truncating bits in some kind of weird double optimization.

所以我是R中的一个巨大的data.table粉丝。我几乎一直使用它,但遇到了一个根本不适合我的情况。我有一个包(我公司内部)使用R的double来存储无符号64位整数的值,其位序列对应于某些奇特的编码。这个包在除data.table之外的任何地方都可以很好地工作。我发现,如果我在这个数据的列上聚合,我会丢失大量的唯一值。我唯一的猜测是data.table在某种奇怪的双重优化中截断位。

Can anyone confirm that this is the case? Is this simply a bug?

任何人都可以确认是这种情况吗?这只是一个错误吗?

Below see a reproduction of the issue and comparison to the package I currently must use but want to avoid with a passion (dplyr).

下面看到问题的复制和我目前必须使用的包装的比较,但希望避免激情(dplyr)。

temp <- structure(list(obscure_math = c(6.95476896592629e-309, 6.95476863436446e-309, 
6.95476743245288e-309, 6.95476942182375e-309, 6.95477149408563e-309, 
6.95477132830476e-309, 6.95477132830476e-309, 6.95477149408562e-309, 
6.95477174275702e-309, 6.95476880014538e-309, 6.95476896592647e-309, 
6.95476896592647e-309, 6.95476900737172e-309, 6.95476900737172e-309, 
6.95476946326899e-309, 6.95476958760468e-309, 6.95476958760468e-309, 
6.95477020928318e-309, 6.95477124541406e-309, 6.95476859291965e-309, 
6.95476875870014e-309, 6.95476904881676e-309, 6.95476904881676e-309, 
6.95476904881676e-309, 6.95476909026199e-309, 6.95476909026199e-309, 
6.95476909026199e-309, 6.95476909026199e-309, 6.9547691317072e-309, 
6.9547691317072e-309, 6.9547691317072e-309, 6.9547691317072e-309, 
6.9547691317072e-309, 6.9547691317072e-309, 6.9547691317072e-309, 
6.9547691317072e-309, 6.9547691317072e-309, 6.9547691317072e-309, 
6.9547691317072e-309, 6.9547691317072e-309, 6.95477211576406e-309, 
6.95476880014538e-309, 6.95476880014538e-309, 6.95476880014538e-309, 
6.95476892448104e-309, 6.95476880014538e-309, 6.95476892448105e-309, 
6.9547689659263e-309, 6.95476913170719e-309, 6.95476933893334e-309
)), .Names = "obscure_math", class = c("data.table", "data.frame"), row.names = c(NA, 
-50L))

dt_collapsed <- temp[, .(count=.N), by=obscure_math]
nrow(dt_collapsed) == length(unique(temp$obscure_math))

setDF(temp)
dplyr_collapsed <- temp %>% group_by(obscure_math) %>% summarise(count=n())
nrow(dplyr_collapsed) == length(unique(temp$obscure_math))

1 个解决方案

#1


18  

Update: the default rounding feature has been removed in the current development version of data.table (v1.9.7). See installation instructions for devel version here.

更新:已在当前开发版本的data.table(v1.9.7)中删除了默认的舍入功能。请参阅此处的devel版本的安装说明。

This also means that you're responsible for understanding the limitations in representing floating point numbers and dealing with it.

这也意味着您有责任理解表示浮点数并处理浮点数的限制。


data.table has been around for a long time. We used to deal with limitations in floating point representations by using a threshold (like base R does, e.g., all.equal). However it simply does not work, since it needs to be adaptive depending on how big the numbers compared are. This series of articles is an excellent read on this topic and other potential issues.

data.table已存在很长时间了。我们过去常常通过使用阈值来处理浮点表示的限制(例如,基数R,例如all.equal)。然而它根本不起作用,因为它需要自适应,这取决于比较的数字有多大。本系列文章是关于此主题和其他潜在问题的精彩读物。

This being a recurring issue because a) people don't realise the limitations, or b) thresholding did not really help their issue, meant that people kept asking here or posting on the project page.

这是一个反复出现的问题,因为a)人们没有意识到这些限制,或者b)阈值并没有真正帮助他们解决问题,这意味着人们不断在这里询问或在项目页面上发帖。

While we reimplemented data.table's order to fast radix ordering, we took the opportunity to provide an alternative way of fixing the issue, and providing a way out if it proves undesirable (exporting setNumericRounding). With #1642 issue, ordering probably doesn't need to have rounding of doubles (but it's not that simple, since order directly affects binary search based subsets).

虽然我们重新实现了data.table对快速基数排序的顺序,但我们借此机会提供了另一种解决问题的方法,并提供了一种解决方法,如果它被证明是不合需要的(导出setNumericRounding)。对于#1642问题,排序可能不需要舍入双精度(但它并不那么简单,因为顺序直接影响基于二进制搜索的子集)。

The actual problem here is grouping on floating point numbers, even worse is such numbers as in your case. That is just a bad choice IMHO.

这里的实际问题是对浮点数进行分组,更糟糕的是在你的情况下这样的数字。这只是一个糟糕的选择恕我直言。

I can think of two ways forward:

我可以想到前进的两种方式:

  1. When grouping on columns that are really doubles (in R, 1 is double as opposed to 1L, and those cases don't have issues) we provide a warning that the last 2 bytes are rounded off, and that people should read ?setNumericRounding. And also suggest using bit64::integer64.

    当对真正加倍的列进行分组时(在R中,1是双倍而不是1L,并且这些情况没有问题),我们提供一个警告,即最后2个字节被舍入,并且人们应该读取?setNumericRounding。并且还建议使用bit64 :: integer64。

  2. Remove the functionality of allowing grouping operations on really double values or force them to fix the precision to certain digits before continuing. I can't think of a valid reason why one would want to group by floating point numbers really (would love to hear from people who do).

    删除允许对实际双值进行分组操作的功能,或强制它们在继续之前将精度固定为某些数字。我想不出一个有效的理由,为什么人们会想要通过浮点数进行分组(很想听到那些做过的人)。

What is very unlikely to happen is going back to thresholding based checks for identifying which doubles should belong to the same group.

最不可能发生的是回到基于阈值的检查,以确定哪些双打应属于同一组。

Just so that the Q remains answered, use setNumericRounding(0L).

为了使Q保持应答,请使用setNumericRounding(0L)。


推荐阅读
  • FeatureRequestIsyourfeaturerequestrelatedtoaproblem?Please ... [详细]
  • ZSI.generate.Wsdl2PythonError: unsupported local simpleType restriction ... [详细]
  • http:my.oschina.netleejun2005blog136820刚看到群里又有同学在说HTTP协议下的Get请求参数长度是有大小限制的,最大不能超过XX ... [详细]
  • 自动轮播,反转播放的ViewPagerAdapter的使用方法和效果展示
    本文介绍了如何使用自动轮播、反转播放的ViewPagerAdapter,并展示了其效果。该ViewPagerAdapter支持无限循环、触摸暂停、切换缩放等功能。同时提供了使用GIF.gif的示例和github地址。通过LoopFragmentPagerAdapter类的getActualCount、getActualItem和getActualPagerTitle方法可以实现自定义的循环效果和标题展示。 ... [详细]
  • Webpack5内置处理图片资源的配置方法
    本文介绍了在Webpack5中处理图片资源的配置方法。在Webpack4中,我们需要使用file-loader和url-loader来处理图片资源,但是在Webpack5中,这两个Loader的功能已经被内置到Webpack中,我们只需要简单配置即可实现图片资源的处理。本文还介绍了一些常用的配置方法,如匹配不同类型的图片文件、设置输出路径等。通过本文的学习,读者可以快速掌握Webpack5处理图片资源的方法。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 阿,里,云,物,联网,net,core,客户端,czgl,aliiotclient, ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • baresip android编译、运行教程1语音通话
    本文介绍了如何在安卓平台上编译和运行baresip android,包括下载相关的sdk和ndk,修改ndk路径和输出目录,以及创建一个c++的安卓工程并将目录考到cpp下。详细步骤可参考给出的链接和文档。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • ubuntu用sqoop将数据从hive导入mysql时,命令: ... [详细]
  • IjustinheritedsomewebpageswhichusesMooTools.IneverusedMooTools.NowIneedtoaddsomef ... [详细]
  • 本文讨论了编写可保护的代码的重要性,包括提高代码的可读性、可调试性和直观性。同时介绍了优化代码的方法,如代码格式化、解释函数和提炼函数等。还提到了一些常见的坏代码味道,如不规范的命名、重复代码、过长的函数和参数列表等。最后,介绍了如何处理数据泥团和进行函数重构,以提高代码质量和可维护性。 ... [详细]
  • Java SE从入门到放弃(三)的逻辑运算符详解
    本文详细介绍了Java SE中的逻辑运算符,包括逻辑运算符的操作和运算结果,以及与运算符的不同之处。通过代码演示,展示了逻辑运算符的使用方法和注意事项。文章以Java SE从入门到放弃(三)为背景,对逻辑运算符进行了深入的解析。 ... [详细]
  • 本文介绍了PHP常量的定义和使用方法,包括常量的命名规则、大小写敏感性、全局范围和标量数据的限制。同时还提到了应尽量避免定义resource常量,并给出了使用define()函数定义常量的示例。 ... [详细]
author-avatar
龙帅1314的爱_530
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有