[R] string parsing

Sam Steingold sds at gnu.org
Wed Feb 16 20:09:06 CET 2011


> * David Winsemius <qjvafrzvhf at pbzpnfg.arg> [2011-02-16 13:33:32 -0500]:
>
>> parse.num <- function (s) {
>> as.numeric(gsub("M$","e6",gsub("B$","e9",s))); }
>
> data[1] <- parse.num( data[[1]] )  # as.numeric and gsub are vectorized

because parse.num turned out to not be as simple as that: I need to
handle "N/A" specially.

parse.num1 <- function (s) {
  if (length(s) != 1) stop("parse.num",s);
  s <- as.character(s);
  if (s == "N/A") return(NA);
  as.numeric(gsub("M$","e6",gsub("B$","e9",s)));
}

parse.num <- function (v) {
  for (i in 1:length(v)) v[[i]] <- parse.num1(v[[i]])
  v;
}

actually... wait a sec...
shouldn't this work?

  bad <- (data[1] == "N/A")
  data[1][bad] <- NA
  data[1][!bad] <- as.numeric(gsub("M$","e6",gsub("B$","e9",data[1])));


-- 
Sam Steingold (http://sds.podval.org/) on CentOS release 5.3 (Final)
http://dhimmi.com http://memri.org http://truepeace.org http://camera.org
http://thereligionofpeace.com http://palestinefacts.org http://www.memritv.org
Heck is a place for people who don't believe in gosh.



More information about the R-help mailing list