Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R cleaning up a character and converting it into a numeric

Tags:

r

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.

like image 805
adam.888 Avatar asked Mar 16 '12 14:03

adam.888


2 Answers

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)
like image 68
Ari B. Friedman Avatar answered Oct 17 '22 22:10

Ari B. Friedman


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"
like image 5
Ben Avatar answered Oct 17 '22 21:10

Ben