导入(64位)具有多个数字的整数时,R中的奇怪错误

Zub*_*bin 8 precision r digits read.csv

我正在导入一个包含一个包含很长整数的列的csv(例如:2121020101132507598)

一个<-read.csv( 'temp.csv',as.is = T)

当我将这些整数作为字符串导入时,它们会正确地通过,但是当作为整数导入时,最后几位数字会被更改.我不知道发生了什么......

1 "4031320121153001444" 4031320121153001472
2 "4113020071082679601" 4113020071082679808
3 "4073020091116779570" 4073020091116779520
4 "2081720101128577687" 2081720101128577792
5 "4041720081087539887" 4041720081087539712
6 "4011120071074301496" 4011120071074301440
7 "4021520051054304372" 4021520051054304256
8 "4082520061068996911" 4082520061068997120
9 "4082620101129165548" 4082620101129165312

Jos*_*ich 11

正如其他人所说,你不能代表那么大的整数.但是R并没有将这些值读成整数,而是将它们读入双精度数值.

双精度只能准确地将数字表示到~16个位置,这就是为什么你看到你的数字在16个位置之后四舍五入.有关可能的解决方案,请参阅gmp,Rmpfrint64包.虽然我没有看到任何一个文件中的文件读取功能,但也许你可以通过查看它们的来源来烹饪.

更新:以下是如何将文件放入int64对象的方法:

# This assumes your numbers are the only column in the file
# Read them in however, just ensure they're read in as character
a <- scan("temp.csv", what="")
ia <- as.int64(a)
Run Code Online (Sandbox Code Playgroud)


小智 7

R的最大整数值约为2E9.正如@Joshua在另一个答案中提到的,其中一个潜在的解决方案是int64包.

将值导入为字符.然后转换为int64类型.

require(int64)
a <- read.csv('temp.csv', colClasses = 'character', header=FALSE)[[1]]
a <- as.int64(a)
print(a)
[1] 4031320121153001444 4113020071082679601 4073020091116779570
[4] 2081720101128577687 4041720081087539887 4011120071074301496
[7] 4021520051054304372 4082520061068996911 4082620101129165548
Run Code Online (Sandbox Code Playgroud)