How to set ',' as decimal separator with R

后端 未结 3 1759
陌清茗
陌清茗 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: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.

提交回复
热议问题