如何用R设置','作为小数分隔符

时间:2022-09-18 07:28:59

Even though my Windows 7 locale settings specify using "," as a decimal separator, R and RStudio give me a "." separator. Is there any way to change this? Note the "LC_NUMERIC=C" setting in the locale below: this seems to be forced by R or RStudio.

即使我的Windows 7区域设置指定使用“,”作为小数分隔符,R和RStudio也给我一个“。”分隔器。有没有办法改变这个?请注意下面语言环境中的“LC_NUMERIC = C”设置:这似乎是由R或RStudio强制执行的。

As I am in the middle of a long project, I am unwilling to change right away to R 3.0 and the last RStudio version. Does anybody know is there is any change regarding the decimal separator issue in these versions?

由于我处于漫长的项目中,我不愿意立即改为R 3.0和最后一个RStudio版本。有谁知道这些版本中的小数分隔符问题是否有任何变化?

I am using prettyNum to solve the problem for single numbers, but I don't know how to use it on a table.

我使用prettyNum解决单个数字的问题,但我不知道如何在表上使用它。

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

sessionInfo()R版本2.15.3(2013-03-01)平台:x86_64-w64-mingw32 / x64(64位)

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  

3 个解决方案

#1


14  

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

read.table和write.table函数(以及它们的大多数表兄弟)使用的小数分隔符设置为“dec”参数。 read.csv2是一种特殊情况,其中dec的默认值为“,”,字段分隔符(“sep”)设置为“;”。

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

您可以更改R打印,绘图和as.character功能的操作的输出。您可以使用以下命令更改默认值:

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

This will NOT cause R to handle numeric input from the console differently. That is hard-coded to "." as the decimal separator.

这不会导致R以不同方式处理来自控制台的数字输入。这是硬编码为“。”作为小数点分隔符。

If you applied a text function to a table object, you would be possibly coercing from a 'numeric' to a 'character' mode, since table objects in R inherit from the "matrix" class.

如果将文本函数应用于表对象,则可能会从“数字”模式强制转换为“字符”模式,因为R中的表对象继承自“矩阵”类。

#2


2  

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

为什么你要使用“,”作为小数分隔符,在这种情况下R将如何插入这个R表达式

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

So, I assume that you just want to override the default decimal separator to print an output and in this case, I think prettyNum is the right tool.

所以,我假设你只想覆盖默认的小数分隔符来打印输出,在这种情况下,我认为prettyNum是正确的工具。

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

#3


0  

Based on the fact that you want to use it with (Pandoc) markdown as far as I can see from the blog comment where you referenced this question, I would also suggest to give a try to my pander package:

基于你想要将它与(Pandoc)markdown一起使用的事实,就我在你引用这个问题的博客评论中所看到的,我还建议试试我的pander包:

> 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 for easier usage with knitr:

或者PHP Markdown Extra语法,以便于使用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   |

#1


14  

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

read.table和write.table函数(以及它们的大多数表兄弟)使用的小数分隔符设置为“dec”参数。 read.csv2是一种特殊情况,其中dec的默认值为“,”,字段分隔符(“sep”)设置为“;”。

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

您可以更改R打印,绘图和as.character功能的操作的输出。您可以使用以下命令更改默认值:

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

This will NOT cause R to handle numeric input from the console differently. That is hard-coded to "." as the decimal separator.

这不会导致R以不同方式处理来自控制台的数字输入。这是硬编码为“。”作为小数点分隔符。

If you applied a text function to a table object, you would be possibly coercing from a 'numeric' to a 'character' mode, since table objects in R inherit from the "matrix" class.

如果将文本函数应用于表对象,则可能会从“数字”模式强制转换为“字符”模式,因为R中的表对象继承自“矩阵”类。

#2


2  

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

为什么你要使用“,”作为小数分隔符,在这种情况下R将如何插入这个R表达式

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

So, I assume that you just want to override the default decimal separator to print an output and in this case, I think prettyNum is the right tool.

所以,我假设你只想覆盖默认的小数分隔符来打印输出,在这种情况下,我认为prettyNum是正确的工具。

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

#3


0  

Based on the fact that you want to use it with (Pandoc) markdown as far as I can see from the blog comment where you referenced this question, I would also suggest to give a try to my pander package:

基于你想要将它与(Pandoc)markdown一起使用的事实,就我在你引用这个问题的博客评论中所看到的,我还建议试试我的pander包:

> 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 for easier usage with knitr:

或者PHP Markdown Extra语法,以便于使用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   |