How to set ',' as decimal separator with R

Despite the fact that in the Windows 7 locale settings, specify "," as a decimal separator, R and RStudio give me a ".". delimiter. Is there any way to change this? Note the setting of "LC_NUMERIC = C" in the locale below: this is apparently forced by R or RStudio.

Since I'm in the middle of a long project, I don’t want to immediately change it to R 3.0 and the latest version of RStudio. Does anyone know if there are any changes to the decimal separator question in these versions?

I use prettyNum to solve the problem for single numbers, but I do not know how to use it on the table.

sessionInfo () R version 2.15.3 (2013-03-01) Platform: x86_64-w64-mingw32 / x64 (64-bit)

locale: [1] LC_COLLATE=Spanish_Argentina.1252 LC_CTYPE=Spanish_Argentina.1252 [3] LC_MONETARY=Spanish_Argentina.1252 LC_NUMERIC=C [5] LC_TIME=Spanish_Argentina.1252 attached base packages: [1] grid splines stats graphics grDevices utils datasets methods [9] base other attached packages: [1] ascii_2.1 randomForest_4.6-7 pander_0.3.3 fpc_2.1-5 [5] flexmix_2.3-10 mclust_4.0 cluster_1.14.3 MASS_7.3-23 [9] Gmisc_0.5.0.0 testthat_0.7 boot_1.3-7 rms_3.6-3 [13] miscTools_0.6-16 stringr_0.6.2 Hmisc_3.10-1 survival_2.37-2 [17] lattice_0.20-13 xtable_1.7-1 pixmap_0.4-11 RColorBrewer_1.0-5 [21] ade4_1.5-1 loaded via a namespace (and not attached): [1] digest_0.6.3 evaluate_0.4.3 modeltools_0.2-19 stats4_2.15.3 [5] tools_2.15.3 
+6
source share
3 answers

The decimal separator used by the read.table and write.table (and most of their cousins) is specified by the dec parameter. read.csv2 is a special case where the default value for dec is "," and the field separator ("sep") is set to ";".

You can change the output from R-printing, plotting and actions of the as.character function. You change it by default:

  options(OutDec= ",") # read ?options print( pi ) #[1] 3,141593 options(OutDec= ",") # restore default value 

This will NOT cause R to handle numerical input from the console differently. It is hardcoded for "." as a decimal separator.

If you applied a text function to a table object, you could force a switch from "numeric" to "character" mode, since table objects in R inherit from the "matrix" class.

+11
source

Why do you want to use "," as a decimal separator, in this case, how R will display this expression R

 x <- c(2,3) # (two vectors or one). 

So, I assume that you just want to override the default decimal separator for printing output, in which case I think prettyNum is the right tool.

 require(plyr) head(numcolwise(prettyNum)(iris, dec = ",")) ## Sepal.Length Sepal.Width Petal.Length Petal.Width ## 1 5,1 3,5 1,4 0,2 ## 2 4,9 3 1,4 0,2 ## 3 4,7 3,2 1,3 0,2 ## 4 4,6 3,1 1,5 0,2 ## 5 5 3,6 1,4 0,2 ## 6 5,4 3,9 1,7 0,4 
+2
source

Based on the fact that you want to use it with markdowns (Pandoc), as far as I can see from the comment where you referred to this question , I would also suggest trying my pander package:

 > library(pander) > panderOptions('decimal.mark', ',') > panderOptions('table.split.table', Inf) > pander(head(iris)) ------------------------------------------------------------------- Sepal.Length Sepal.Width Petal.Length Petal.Width Species -------------- ------------- -------------- ------------- --------- 5,1 3,5 1,4 0,2 setosa 4,9 3 1,4 0,2 setosa 4,7 3,2 1,3 0,2 setosa 4,6 3,1 1,5 0,2 setosa 5 3,6 1,4 0,2 setosa 5,4 3,9 1,7 0,4 setosa ------------------------------------------------------------------- 

Or PHP Markdown Extra syntax to simplify use with knitr :

 > pandoc.table(head(iris), style = 'rmarkdown') | Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species | |:--------------:|:-------------:|:--------------:|:-------------:|:---------:| | 5,1 | 3,5 | 1,4 | 0,2 | setosa | | 4,9 | 3 | 1,4 | 0,2 | setosa | | 4,7 | 3,2 | 1,3 | 0,2 | setosa | | 4,6 | 3,1 | 1,5 | 0,2 | setosa | | 5 | 3,6 | 1,4 | 0,2 | setosa | | 5,4 | 3,9 | 1,7 | 0,4 | setosa | 
0
source

Source: https://habr.com/ru/post/944310/


All Articles