我正在尝试使用data.table
的fread
读取CSV,其中小数点是,
。它返回关于更改系统区域设置的错误。
你知道怎么做吗?
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
我遇到这个问题是因为在Ubuntu中没有安装区域设置fr_FR.utf8
。您可以在系统shell中使用sudo命令安装它:
sudo locale-gen fr_FR.UTF-8
然后重新启动r
希望有所帮助