JimminyCricket
JimminyCricket

Reputation: 381

How to convert floating point numbers in R

I have been trying to convert the following to decimal floating point numbers with no luck.

x <- c("0,989" "0,990" "0,991" "0,990" "0,989" "0,989" "0,990" "0,990" "0,989")

These are characters in my data frame but as.numeric(as.character(x)) doesn't help. It generates bunch of NAs.

I would appreciate if someone can help.

Upvotes: 0

Views: 515

Answers (1)

93i7hdjb
93i7hdjb

Reputation: 1196

This is because of the commas in the string. Replace the commas using gsub like so:

new <- gsub(",", ".", x)

Then you can convert to numeric

result <- as.numeric(new)

Upvotes: 2

Related Questions