Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting dot to comma in numeric

If I have a data frame:

a   b  
2.4 4.6
3.6 66.7
5.8 44.6

Both a and b are numeric.

I want to convert "." to "," 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?

like image 808
Manuel Frias Avatar asked Nov 26 '14 20:11

Manuel Frias


People also ask

How do you replace a dot with a comma in Excel?

If you need to replace the full stop with a comma in an excel spreadsheet, use edit / find. Highlight the range in which you need to replace, use "edit" "find" and enter full stop and comma, then click on replace.


3 Answers

Your initial idea was almost correct, just regular expression was wrong, because . matches any symbol. You need something like (this will convert numeric vector to a character vector)

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

Also 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= ",")

And another option is using format function.

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

I assume that you care about how numbers are printed (output), because internally numeric is stored as a double precision floating point number (Update thanks to comment by @digemall). Also unless for some function like read.table it is specifically specified that decimal separator is ,, it's not possible to do otherwise, because by default , is used for separating function arguments.

And NA are introduced exactly for that reason (aside from incorrect regex).

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

By default parser does not know that , is used as a decimal separator.

like image 147
romants Avatar answered Oct 17 '22 03:10

romants


If you only want commas for printing, you can use format:

data <- data.frame(a=rnorm(5), b=rnorm(5))
format(data, decimal.mark=",")
              a          b
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 will still have to use dots in assigns.

like image 24
Karolis Koncevičius Avatar answered Oct 17 '22 04:10

Karolis Koncevičius


Are you on OSX or Windows or ...?

To change the representation, you want to look at the LC_NUMERIC parameter, although R documentation warns that changing this may cause R to operate strangely (hard to use , as decimal when 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

Might be safer just to live with it!

like image 25
beroe Avatar answered Oct 17 '22 04:10

beroe