> * David Winsemius <qjvafrz...@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. ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.