读取大量数字时使用 fread(data.table in R)的错误?
A bug with using fread (data.table in R) when reading large numbers?
这里的目的是读取一个csvtable,文件直接URL。
我想使用 fread(data.table 包),因为它使用 read.csv 更快,但我有一个小问题。
options(scipen=999)
caracteristiques=read.csv(url("https://www.data.gouv.fr/s/resources/base-de-donnees-accidents-corporels-de-la-circulation/20160909-181230/caracteristiques_2015.csv"))
caracteristiques[1,1]
# 201500000001
我无法获取 [1,1] 元素。
现在我使用 fread:
library(data.table)
caracteristiques=data.table(fread("https://www.data.gouv.fr/s/resources/base-de-donnees-accidents-corporels-de-la-circulation/20160909-181230/caracteristiques_2015.csv",
sep=","))
caracteristiques[1,1]
#
然后我们可以看到一个奇怪的数字。我必须指定 options(scipen=0)
才能显示它 9.955423e-313
我想知道是否必须在 fread 中指定一些选项,因为它们在第一列中的数字很大。
fread
自动假定第一列的 class 为 integer64。来自其帮助文件:
integer64
= "integer64" (default) reads columns detected as containing integers
larger than 2^31 as type bit64::integer64. Alternatively,
"double"|"numeric" reads as base::read.csv does; i.e., possibly with
loss of precision and if so silently. Or, "character".
第一列的值是:201500000001、201500000002等,如果你把它们当成数字,它们都大于2^31(即2147483648)。因此 fread
将它们解释为 integer64
值,并导致它们看起来很奇怪。
在这种情况下,data.table 会自动为您加载 bit64
包,以便正确显示数字。但是,当您没有安装 bit64
时(您可能没有),它应该会警告您并要求您安装它。缺少警告是开发版本 v1.10.5 中的错误修复 5。来自 NEWS :
When fread() and print() see integer64 columns are present but package bit64 is not installed, the warning is now displayed as intended. Thanks to a question by Santosh on r-help and forwarded by Bill Dunlap.
所以,只要 install.packages("bit64")
就可以了。您不需要重新加载数据。它只会影响这些列的打印方式。
或者,如果您将参数 integer64 = "numeric"
添加到 fread
函数,结果将与您从 read.csv
获得的结果相匹配。但如果它是一个 ID 列,从概念上讲它应该是一个字符或因子,而不是整数。您可以为此使用参数 colClasses=c("Num_Acc"="character")
。
这里的目的是读取一个csvtable,文件直接URL。 我想使用 fread(data.table 包),因为它使用 read.csv 更快,但我有一个小问题。
options(scipen=999)
caracteristiques=read.csv(url("https://www.data.gouv.fr/s/resources/base-de-donnees-accidents-corporels-de-la-circulation/20160909-181230/caracteristiques_2015.csv"))
caracteristiques[1,1]
# 201500000001
我无法获取 [1,1] 元素。
现在我使用 fread:
library(data.table)
caracteristiques=data.table(fread("https://www.data.gouv.fr/s/resources/base-de-donnees-accidents-corporels-de-la-circulation/20160909-181230/caracteristiques_2015.csv",
sep=","))
caracteristiques[1,1]
#
然后我们可以看到一个奇怪的数字。我必须指定 options(scipen=0)
才能显示它 9.955423e-313
我想知道是否必须在 fread 中指定一些选项,因为它们在第一列中的数字很大。
fread
自动假定第一列的 class 为 integer64。来自其帮助文件:
integer64
= "integer64" (default) reads columns detected as containing integers larger than 2^31 as type bit64::integer64. Alternatively, "double"|"numeric" reads as base::read.csv does; i.e., possibly with loss of precision and if so silently. Or, "character".
第一列的值是:201500000001、201500000002等,如果你把它们当成数字,它们都大于2^31(即2147483648)。因此 fread
将它们解释为 integer64
值,并导致它们看起来很奇怪。
data.table 会自动为您加载 bit64
包,以便正确显示数字。但是,当您没有安装 bit64
时(您可能没有),它应该会警告您并要求您安装它。缺少警告是开发版本 v1.10.5 中的错误修复 5。来自 NEWS :
When fread() and print() see integer64 columns are present but package bit64 is not installed, the warning is now displayed as intended. Thanks to a question by Santosh on r-help and forwarded by Bill Dunlap.
所以,只要 install.packages("bit64")
就可以了。您不需要重新加载数据。它只会影响这些列的打印方式。
或者,如果您将参数 integer64 = "numeric"
添加到 fread
函数,结果将与您从 read.csv
获得的结果相匹配。但如果它是一个 ID 列,从概念上讲它应该是一个字符或因子,而不是整数。您可以为此使用参数 colClasses=c("Num_Acc"="character")
。