为什么将数字转换为字符会更改数字? [英] Why can converting numbers to characters change the numbers?
问题描述
我想这与R的数据结构有关,答案很快,但是我还没有找到一个答案,
I imagine this has to do with R's data structures and the answer will be quick, but I haven't yet found one so here goes:
as.character(9875987598759875)
[1] "9875987598759876"
library(crayon)
chr(9875987598759875)
[1] "9875987598759876"
toString(9875987598759875)
[1] "9875987598759876"
有什么作用?我应该如何更安全地进行此转换?
What gives? How should I be making this conversion more safely?
推荐答案
.Machine $ integer.max
表示R可以存储的最大整数是2147483647(可以想象在不同平台上会有所不同,但这是不太可能的).大于该数字的任何数字都会自动转换为浮点,并伴有不精确/舍入错误.(与Python不同,Python昂贵但不可思议地根据需要将整数变量转换为任意长度的表示形式.)
.Machine$integer.max
indicates that the largest integer R can store is 2147483647 (this could conceivably vary across platforms, but it's very unlikely to). Any number larger than that is automatically converted to floating point, with the attendant imprecision/round-off error. (Unlike in Python, which expensively but magically converts integer variables to an arbitrary-length representation as necessary.)
如果安装了 bit64
程序包,则可以使用64位整数,其准确度最高(大概)
If you install the bit64
package you can use 64-bit integers, with (presumably) exactness up to
print(2^63-1,digits=22)
[1] 9223372036854775808
如果您开始使用字符串,则可以安全地往返转换为 integer64
并返回:
If you start with a character string, you can safely do round-trip conversion to integer64
and back:
library(bit64)
cc <- "9875987598759875"
x <- as.integer64(cc)
identical(cc,as.character(x))
## [1] TRUE
但是,通常,一旦您将数字读入R作为常规数字,就为时已晚.您可以将 colClasses =" integer64"
与 read.table()
/ read.csv()
/etc一起使用.以 integer64
的形式读取值;我相信 readr
和 data.table
的文件读取功能也具有integer64处理功能.
However, typically once you've read a number into R as a regular number it's too late. You can use colClasses="integer64"
with read.table()
/read.csv()
/etc. to read values in as integer64
; I believe the file-reading functions from readr
and data.table
also have integer64-handling capabilities.
对于许多应用程序,如果您实际上并不打算使用这些数字字符串进行任何数值运算,那么首先确保将它们导入为 character
是最安全,最简单的方法...
For many applications, if you're not actually planning on doing anything numerical with these digit-strings, it's safest and easiest to make sure you import them as character
in the first place ...
这篇关于为什么将数字转换为字符会更改数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!