R 如何在不丢失信息的情况下将数字转换为数字
在我的数据中,有一列包含长值,如以下数字:R 如何在不丢失信息的情况下将数字转换为数字,r,R,在我的数据中,有一列包含长值,如以下数字:146953935218 当我将其转换为数字时,它会丢失信息,并变成如下数字:1.469534e+12如果我们需要读取具有大整数列的数据集,请在使用fread之前加载位64 library(bit64) library(data.table) dt1 <- fread("yourfile.csv") 库(位64) 库(数据表) dt1将信息转换为数字时,不会丢失信息。请参阅选项(“数字”)。这只是印刷问题。尝试设置选项(数字=20)然后在控制台中
146953935218
当我将其转换为数字时,它会丢失信息,并变成如下数字:
1.469534e+12
如果我们需要读取具有大整数列的数据集,请在使用fread
之前加载位64
library(bit64)
library(data.table)
dt1 <- fread("yourfile.csv")
库(位64)
库(数据表)
dt1将信息转换为数字时,不会丢失信息。请参阅选项(“数字”)
。这只是印刷问题。尝试设置选项(数字=20)
然后在控制台中键入146953935218
在控制台中键入146953935218L
时(后缀L
告诉R您想要的是整数而不是双精度
),您会得到:
因为这样的整数在32位模式下是不可表示的。?integer
的“详细信息”部分给出:
Integer vectors exist so that data can be passed to C or Fortran
code which expects them, and so that (small) integer data can be
represented exactly and compactly.
Note that current implementations of R use 32-bit integers for
integer vectors, so the range of representable integers is
restricted to about +/-2*10^9: ‘double’s can hold much larger
integers exactly.
最后一句话也告诉你,没有任何准确性会丢失
如果你想把146953935218
作为一个整数,你需要@akrun建议使用64位表示法,只要你在一台64位机器上使用64位R。但我觉得没有必要这样做
您还需要记住存储成本。如果将数据保留为双精度
,则每个数字仍为32位;如果将数据保留为64位整数,则会使内存使用量翻倍。事实上,如果以后使用这些数字执行浮点计算,则需要在double
模式下使用它们。那么为什么不直接使用double
?试试选项(scipen=999)
?您不需要调用库(bit64)
来正确读取fread
(默认情况下是这样),但打印它可能更好。从文档中:“integer64”(默认)读取检测到包含大于2^31的整数的列,类型为bit64::integer64。
@docendodiscimus我有一个很大的事务数据集,我以前用fread
读取该数据集时,ID是非常长的整数。我原以为它会正确拾取,但当我检查是否有重复时,出现了一些问题。因此,我用第64位再次阅读了它,它得到了解决。
[1] 1.469534e+12
Warning message:
non-integer value 1469533935218L qualified with L; using numeric value
Integer vectors exist so that data can be passed to C or Fortran
code which expects them, and so that (small) integer data can be
represented exactly and compactly.
Note that current implementations of R use 32-bit integers for
integer vectors, so the range of representable integers is
restricted to about +/-2*10^9: ‘double’s can hold much larger
integers exactly.