Convert dots to commas in numeric

If I have a data frame:

ab 2.4 4.6 3.6 66.7 5.8 44.6 

Both a and b are numeric.

I want to convert "." on "," with

 df$a <- as.numeric(gsub(".", ",", df$a)) 

but i always get

 Warning message:NAs introduced by coercion 

and all values ​​are converted to NA. Why?

+5
source share
3 answers

Your initial idea was almost correct, just the regular expression was wrong because . matches any character. You need something like (this converts a number vector to a character vector)

 df$a <- gsub("\\.", ",", df$a) 

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

options(OutDec= ",")

And another option uses the format function.

 format(df, decimal.mark=",") 

I assume that you care about how numbers are printed (output), since the internal numeric value is stored as a double-precision floating-point number ( Update thanks to @digemall's comment). In addition, if for some function, for example read.table , it is specifically indicated that the decimal separator, it cannot be done otherwise, because by default , used to separate the arguments of the function.

And NA introduced precisely for this reason (except for incorrect regular expression).

 df$a <- as.numeric(gsub("\\.", ",", df$a)) 

By default, the parser does not know what , used as a decimal separator.

+14
source

If you need only commas for printing, you can use the format:

 data <- data.frame(a=rnorm(5), b=rnorm(5)) format(data, decimal.mark=",") ab 1 1,058878354 0,1812629 2 1,026163906 -0,6666500 3 1,538423889 -1,4206752 4 -0,561585916 -0,4729558 5 -0,004685406 1,0744514 

However, this will only change how they look. You still have to use points in appointments.

+2
source

Are you on OSX or Windows or ...?

To change the view, you want to look at the LC_NUMERIC parameter, although the R documentation warns that changing this can cause R to work weird (hard to use as a decimal if it is also used to define lists ...)

 > Sys.getlocale("LC_NUMERIC") [1] "C" > a=c(1.01,2.01) > a [1] 1.01 2.01 > Sys.setlocale("LC_NUMERIC", "de_DE") # this is OSX syntax > a [1] 1,01 2,01 

You can be safer to live with it!

+1
source

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


All Articles