'How to convert character argument (decimal number) to numeric in R?

I want to convert for example the number 167009345.8 to 167009345.8.

I have used lots of ways but I have problems.

For example,

x <- "167009345.8"
class(x) <- "numeric"`

the output is 167009346.

But I want the decimal number 167009345.8.

I have used also as.numeric, but I have the same problem.

Could you please help me?



Solution 1:[1]

I had to split a long character string into separate decimal numbers. It was very frustrating. Maybe this can spare some others some time:

string = as.character("1.23456, -2.34567, -8.90, +0, +99999.9999, -0.0")
charlist = strsplit(string, "," )
numberlist = lapply(charlist, function(x) (as.numeric(x)))
vector = as.numeric(unlist(numberlist))
vector
[1]      1.23456     -2.34567     -8.90000      0.00000  99999.99990      0.00000

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1