data.table :: fread中的dec参数 [英] dec argument in data.table::fread

查看:199
本文介绍了data.table :: fread中的dec参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 fread data.table 加载csv文件。但是我的csv文件使用 dec =,作为小数分隔符( 1.23 1,23 )。与 read.csv 不同, dec 不是允许的参数。

I am using fread from data.table to load csv files. However my csv files uses dec="," as a decimal-separator (1.23 will be 1,23). Unlike in read.csv it seems that dec is not an allowed parameter.

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 (它是如此快得多,它节省了我很多时间)?

Do you see a work around (a R option to set may be) that will enable me to use fread (it is so much faster that it saves me a lot of time)?

PS: colClasses 尚未实现,所以 setAs 不能像此帖中使用

PS: colClasses is not yet implemented so setAs cannot be used like in this post

推荐答案

2014年10月更新:现在在v1.9.5

Update Oct 2014 : Now in v1.9.5

fread 现在接受 dec =','十进制分隔符),#917 。新的段落已添加到?fread 。如果你位于使用 dec =','的国家,那么它应该工作。如果没有,你将需要阅读段落一个额外的步骤。如果它以某种方式断开 dec ='。',这个新功能可以通过选项(datatable.fread.dec.experiment = FALSE)

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).









上一个答案...



Previous answer ...

Matt Dowle发现了一个使用语言环境的好工作。
第一个 sessionInfo

Matt Dowle found a nice work-around with locales. First my 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
...

尝试以下显示了罪魁祸首:

Trying the following shows the culprit:

Sys.localeconv()["decimal_point"]
decimal_point 
          "." 

尝试设置LC_NUMERIC在Ubuntu(Matthew)和WinXP(me)上工作

Trying to set the LC_NUMERIC worked on Ubuntu(Matthew) and 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

这篇关于data.table :: fread中的dec参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆