Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

decimal point setting in fread, data.table

Tags:

r

data.table

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.

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

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

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

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

I get a warning message:

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

, 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  
like image 957
Henk Avatar asked Nov 13 '13 15:11

Henk


1 Answers

Update Oct 2014 : Now in 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).



Previous answer ...

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

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

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

like image 71
eddi Avatar answered Oct 20 '22 12:10

eddi