我正在使用 data.table
中的 fread
加载 csv 文件。但是,我的 csv 文件使用 dec=","
作为小数分隔符(1.23
将是 1,23
)。与 read.csv
不同,dec
似乎不是允许的参数。
R) args(fread)
function (input = "test.csv", sep = "auto", sep2 = "auto", nrows = -1,
header = "auto", na.strings = "NA", stringsAsFactors = FALSE,
verbose = FALSE, autostart = 30)
您是否看到一种解决方法(可能需要设置 R 选项),使我能够使用 fread
(它速度快得多,可以节省我很多时间)?
PS:colClasses
尚未实现,因此setAs
不能像this post中那样使用
最佳答案
2014 年 10 月更新:现已推出 v1.9.5
<小时/> <小时/>
fread
now acceptsdec=','
(and other non-'.' decimal separators), #917. A new paragraph has been added to?fread
. If you are located in a country that usesdec=','
then it should just work. If not, you will need to read the paragraph for an extra step. In case it somehow breaksdec='.'
, this new feature can be turned off withoptions(datatable.fread.dec.experiment=FALSE)
.
之前的回答...
Matt Dowle 找到了一个很好的语言环境解决方法。
首先是我的 sessionInfo
sessionInfo()
R version 2.15.2 (2012-10-26)
Platform: i386-w64-mingw32/i386 (32-bit)
locale:
[1] LC_COLLATE=French_France.1252 LC_CTYPE=French_France.1252 LC_MONETARY=French_France.1252 LC_NUMERIC=C
[5] LC_TIME=C
...
尝试以下操作可以找出罪魁祸首:
Sys.localeconv()["decimal_point"]
decimal_point
"."
尝试设置 LC_NUMERIC 在 Ubuntu(Matthew) 和 WinXP(me) 上有效
Sys.setlocale("LC_NUMERIC", "French_France.1252")
[1] "French_France.1252"
Message d'avis :
In Sys.setlocale("LC_NUMERIC", "French_France.1252") :
changer 'LC_NUMERIC' peut résulter en un fonctionnement étrange de R
行为很好,变化如下:
DT = fread("A,B\n3,14;123\n4,22;456\n",sep=";")
str(DT)
Classes ‘data.table’ and 'data.frame': 2 obs. of 2 variables:
$ V1: num 3.14 4.22
$ V2: int 123 456
“.”小数点分隔符现在作为字符串加载(理应如此),与之前相反。
DT = fread("A,B\n3.14;123\n4.22;456\n",sep=";")
str(DT)
Classes ‘data.table’ and 'data.frame': 2 obs. of 2 variables:
$ V1: chr "3.14" "4.22"
$ V2: int 123 456
关于r - data.table::fread 中的 dec 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14440661/