How to set ',' as decimal separator with R

后端 未结 3 1663
陌清茗
陌清茗 2021-01-04 10:29

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_

相关标签:
3条回答
  • 2021-01-04 11:09

    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:

    > 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:

    > 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 讨论(0)
  • 2021-01-04 11:11

    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 ";".

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

     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.

    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.

    0 讨论(0)
  • 2021-01-04 11:20

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

    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.

    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
    
    0 讨论(0)
提交回复
热议问题