r - data.table::fread 中的 dec 参数

标签 r csv data.table

我正在使用 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 accepts dec=',' (and other non-'.' decimal separators), #917. A new paragraph has been added to ?fread. If you are located in a country that uses dec=',' then it should just work. If not, you will need to read the paragraph for an extra step. In case it somehow breaks dec='.', this new feature can be turned off with options(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/

相关文章:

python - 使用 Python DictReader 获取特定行和值

r - 在世界地图上叠加多个河图(桑基图)

r - 连接 Bloomberg 和 R 的 R 包 "Rblpapi"的正确用法

python - 通过 Mechanize 绕过 404

powershell - 使用DisplayNames从文本文件获取SamAccountName

r - 使用R的data.table用NA替换不可能的值

r - 通过不在该 data.table 中的列通过行扩展 R data.table?

r - 通过函数对 data.table 进行绝对降序排序?

r - 使用 dplyr 按名称省略列来计算行总和

R计算重复值并将它们添加到单独的向量中