R 中的 long/bigint/decimal 等效数据类型 [英] long/bigint/decimal equivalent datatype in R

查看:72
本文介绍了R 中的 long/bigint/decimal 等效数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们必须选择哪些数据类型来处理 R 中的大数?默认情况下,整数的大小似乎是 32 位,因此来自 sql server 的 bigint 数字以及通过 rpy2 从 python 传递的任何大数字都会被破坏.

What datatype choices do we have to handle large numbers in R? By default, the size of an integer seems to be 32bit, so bigint numbers from sql server as well as any large numbers passed from python via rpy2 get mangled.

> 123456789123
[1] 123456789123
> 1234567891234
[1] 1.234568e+12

当使用 RODBC 读取 123456789123456789 的 bigint 值时,它返回为 123456789123456784(见最后一位数字),并且通过 RJSONIO 反序列化时返回相同的数字,返回为 -1395630315L(这似乎是一个额外的 JSON 错误/限制).

When reading a bigint value of 123456789123456789 using RODBC, it comes back as 123456789123456784 (see the last digit), and the same number when deserialized via RJSONIO, comes back as -1395630315L (which seems like an additional bug/limitation of RJSONIO).

> fromJSON('[1234567891]')
[1] 1234567891
> fromJSON('[12345678912]')
[1] -539222976

实际上,我确实需要能够处理来自 JSON 的大量数字,因此由于 RJSONIO 的限制,除了寻找更好的 JSON 库(现在这似乎不是选项)之外,我可能没有其他解决方法.我想听听专家们对此的看法以及一般情况.

Actually, I do need to be able to handle large numbers coming from JSON, so with RJSONIO's limitation, I may not have a workaround except for finding a better JSON library (which seems like a non-option right now). I would like to hear what experts have to say on this as well as in general.

推荐答案

查看help(integer):

 Note that on almost all implementations of R the range of
 representable integers is restricted to about +/-2*10^9: ‘double’s
 can hold much larger integers exactly.

所以我建议使用 numeric(即double")——一个双精度数.

so I would recommend using numeric (i.e. 'double') -- a double-precision number.

这篇关于R 中的 long/bigint/decimal 等效数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆