R cleaning up a character string and converting it into a numeric
I have a character string
abc <- "Â 267750Â"
class(abc)
"character"
What to I need to do to abc to get rid of "Â Â" and convert it from character to numeric. Perhaps as.numeric will work but I need to get rid of "Â Â" first.
I want to convert the above to:
abc
267750
class(abc)
"numeric"
Thank you for your help.
You can parse out what you don't want with regular expressions:
test <- "532.dcx3vds98"
destring <- function(x,keep="0-9.") {
return( as.numeric(gsub(paste("[^",keep,"]+",sep=""),"",x)) )
}
destring(test)
Returns 532.398
.
Edit
This is now in taRifx
:
library(taRifx)
test <- "532.dcx3vds98"
destring(test)
a little shorter using stringr
:
# load library
library(stringr)
# load data
abc <- "Â 267750Â"
# extract digits
abc <- as.numeric(str_extract(abc, "[0-9]+"))
# check the result
abc
[1] 267750
class(abc)
[1] "numeric"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With