fread,data.table中的小数点设置 [英] decimal point setting in fread, data.table

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

问题描述

我想使用data.table中的fread,但会收到与小数点有关的警告[这里是‘,’而不是’。]。通常我使用'。',但是在某些情况下,我必须使用','作为小数点导入文件。

I would like to use fread from data.table, but get a warning related to the decimal point [here a ',' instead of a '.']. Normally I use '.', but in some cases the file I have to import files with ',' as decimal point.

在read.csv中,我可以设置小数点分隔符:

In read.csv I can set the decimal point separator:

df <- read.csv("mydata.csv", sep=";", dec=",")

如何在data.table的fread函数中执行此操作?与

How can I do this in the fread function in data.table? with

df=fread('mydata.csv',sep=';')

我收到警告消息:

Warning message:
In fread("mydata.csv",  :
Bumped column 7 to type character on data row 86, field contains '4,5'. 

,其中4,5是在read.csv中正确读取为'4.5'并且带有sep ='的值。

, where 4,5 is the value the would have been read in correctly as '4.5' with sep=',' in read.csv.

sessionInfo()
R version 3.0.2 (2013-09-25)
Platform: x86_64-pc-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8   
 [6] LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8       LC_NAME=C                  LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C  


推荐答案

2014年10月更新:现在在v1中。 9.5

Update Oct 2014 : Now in v1.9.5


fread 现在接受 dec =',' (和其他非。小数点分隔符),#917 ?fread 中添加了一个新段落。如果您位于使用 dec =’,’的国家/地区,那么它应该可以正常工作。如果没有,您将需要阅读该段落以执行额外的步骤。如果它以某种方式破坏了 dec ='。',可以使用 options(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 ...

由于您使用的是Linux,因此使用 data.table 1.8.11您可以执行以下操作:

Since you're on Linux, using data.table 1.8.11 you can do the following:

fread("sed 's/,/./g' yourfile", sep = ";")

(实际上,我认为您甚至不需要指定 sep 此处)

(actually I don't think you even need to specify sep here)

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

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