简体   繁体   中英

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM