On Feb 16, 2011, at 2:09 PM, Sam Steingold wrote:
* 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);
Ouch! That can be simplified to:
# and then all done inside one function.
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])))
It might not, since at this point you should have different length
vectors.
(I'm also not sure that the listobj[<n>][<logical>] <- <vector>
construction would work, but I really don't know about that one.)
parse.num2 <- function (s) {
is.na(s) <- (s == "N/A")
as.numeric(gsub("M$","e6",gsub("B$","e9",s)))
}
Then call as I suggested
--
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.
David Winsemius, MD
West Hartford, CT
______________________________________________
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.