[R] sd(NA)

hadley wickham h.wickham at gmail.com
Fri Aug 22 21:28:21 CEST 2008


Here's one approach:

try_default <- function(expr, default = NA, quiet = FALSE) {
  result <- default
  if (quiet) {
    tryCatch(result <- expr, error = function(e) {})
  } else {
    try(result <- expr)
  }
  result
}

failwith <- function(default = NULL, f, quiet = FALSE) {
  function(...) try_default(f(...), default, quiet = quiet)
}


sd2 <- failwith(NA, sd)
sd2(NA, na.rm=T)

sd3 <- failwith(NA, sd, quiet = T)
sd3(NA, na.rm=T)

Hadley

-- 
http://had.co.nz/



More information about the R-help mailing list