Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

as.numeric with comma decimal separators?

I have a large vector of strings of the form:

Input = c("1,223", "12,232", "23,0")

etc. That's to say, decimals separated by commas, instead of periods. I want to convert this vector into a numeric vector. Unfortunately, as.numeric(Input) just outputs NA.

My first instinct would be to go to strsplit, but it seems to me that this will likely be very slow. Does anyone have any idea of a faster option?

There's an existing question that suggests read.csv2, but the strings in question are not directly read in that way.

like image 711
Fhnuzoag Avatar asked Mar 05 '13 23:03

Fhnuzoag


People also ask

How do you use a comma with decimals?

And in countries where a point is used as a decimal separator, a comma is usually used to separate thousands. So, for example, twelve thousand five hundred with a decimal of five zero is written differently depending on the country: In the USA, Mexico, or the UK, it would be written: 12 500.50 or 12,500.50.

What is used as separator for decimal numbers?

The character used as the decimal separator In the United States, this character is a period (.). In Germany, it is a comma (,). Thus one thousand twenty-five and seven tenths is displayed as 1,025.7 in the United States and 1.025,7 in Germany.

Is numeric with comma?

In English, we use commas to separate numbers greater than 999. We use a comma every third digit from the right. More than 50,000 people turned up to protest.


Video Answer


2 Answers

as.numeric(sub(",", ".", Input, fixed = TRUE)) 

should work.

like image 57
adibender Avatar answered Oct 04 '22 23:10

adibender


The readr package has a function to parse numbers from strings. You can set many options via the locale argument.

For comma as decimal separator you can write:

readr::parse_number(Input, locale = readr::locale(decimal_mark = ","))
like image 45
tspano Avatar answered Oct 04 '22 23:10

tspano