这里的目的是读取一个csv表,文件有一个直接的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
自动假定第一列的类为 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")
为此。
关于r - 读取大量数字时使用 fread(R 中的 data.table)的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46012189/