[R] vector splicing
Sam Steingold
sds at gnu.org
Thu Feb 17 00:42:17 CET 2011
the following seems to work for numeric but not for times:
parse.vec <- function (vec, parser = as.numeric, vmode = "numeric",
na.string = "N/A") {
bad <- (vec == na.string);
ret <- vector(mode = vmode, length = length(vec));
if (some(bad))
ret[bad] <- NA;
if (some(!bad))
ret[!bad] <- parser(vec[!bad]);
ret;
}
data[[2]] <- parse.vec(data[,2]); # works!
but not with times:
parse.vec(df$Time, vmode = "list", na.string = "<invalid time>",
parser = function (s) strptime(s,format = "%H:%M:%OS"));
Warning message:
In ret[!bad] <- strptime(vec[!bad], format = "%H:%M:%OS") :
number of items to replace is not a multiple of replacement length
why?
(just in case:
> length(ret)
[1] 2022
> length(vec)
[1] 2022
> length(bad)
[1] 2022
> all(!bad)
[1] TRUE
--
Sam Steingold (http://sds.podval.org/) on CentOS release 5.3 (Final)
http://mideasttruth.com http://jihadwatch.org http://truepeace.org
http://dhimmi.com http://memri.org http://pmw.org.il http://camera.org
If it has syntax, it isn't user friendly.
More information about the R-help
mailing list