Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System locale error with fread

I am trying to read a CSV using data.table's fread where the decimal point is ,. It returns an error about changing system locale.

Any ideas how to proceed with that?

sampleCSV = "a;b;c
1,03;80,3;50,4
45,2;65,3;90,678"

library(data.table)
fread(sampleCSV, sep = ';', dec = ",", verbose = T)

# *** Verbose & Error message: ***
# dec=',' but current locale ('C') has dec='.'. Attempting to change locale to one that has the desired decimal point.
# Changing to system locale ('en_US.UTF-8') did not provide the desired dec. Now trying any provided in getOption('datatable.fread.dec.locale')
# Trying 'fr_FR.utf8'
# Sys.setlocale('LC_NUMERIC','fr_FR.utf8')  returned ""; i.e., this locale name is not valid on your system. It was provided by you in getOption("datatable.fread.dec.locale"). See ?Sys.setlocale and ?fread.
# Error in fread(sampleCSV, sep = ";", dec = ",", verbose = T) : 
#   Unable to change to a locale which provides the desired dec. You will need to add a valid locale name to getOption("datatable.fread.dec.locale"). See the long paragraph in ?fread.


Sys.info()
# sysname  release             version                                        machine
# "Linux"  "4.4.0-28-generic" "#47-Ubuntu SMP Fri Jun 24 10:09:13 UTC 2016"  "x86_64"                                    

sessionInfo() 
# R version 3.2.3 (2015-12-10)
# Platform: x86_64-pc-linux-gnu (64-bit)
# Running under: Ubuntu 16.04 LTS
# 
# 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       
# 
# attached base packages:
#   [1] stats     graphics  grDevices utils     datasets  methods   base     
# 
# other attached packages:
#   [1] data.table_1.9.6
# 
# loaded via a namespace (and not attached):
#   [1] tools_3.2.3  chron_2.3-47
like image 556
Deena Avatar asked Oct 31 '22 00:10

Deena


1 Answers

I had this problem because the locale fr_FR.utf8 had not been installed in Ubuntu. You may install it running as sudo the following command in the system shell:

sudo locale-gen fr_FR.UTF-8

And then restarting R.

Hope it helps

like image 74
pau.ferrer Avatar answered Nov 01 '22 12:11

pau.ferrer