Hi All: This was discussed in the R-developers list (see the thread starting at http://tolstoy.newcastle.edu.au/R/e3/devel/ 07/12/0560.html). It has to do with the behavior of sd() when the entire vector is NA. The behavior has changed between 2.6 and 2.7.1 as follows: Run in Version 2.7.1 > tt<-rep(NA, 10) > mean(tt, na.rm=T) [1] NaN >sd(tt, na.rm=T) Error in var(x, na.rm=na.rm) : no complete element pairs Run in Version 2.6.1 > tt<-rep(NA, 10) > mean(tt, na.rm=T) [1] NaN >sd(tt, na.rm=T) Na If I understand the discussion, 2.7.1 fails because 'rm=T' removes the NA's so that it is now empty. What I have been unable to find in any of the mail lists is how you are suppose to program this in 2.7.1. when you are looping through a large number of "variables" (we have time series on a grid and are calculating the mean and sd of the time series at each grid point). Any suggestions much appreciated. -Roy M. ********************** "The contents of this message do not reflect any position of the U.S. Government or NOAA." ********************** Roy Mendelssohn Supervisory Operations Research Analyst NOAA/NMFS Environmental Research Division Southwest Fisheries Science Center 1352 Lighthouse Avenue Pacific Grove, CA 93950-2097 e-mail: Roy.Mendelssohn at noaa.gov (Note new e-mail address) voice: (831)-648-9029 fax: (831)-648-8440 www: http://www.pfeg.noaa.gov/ "Old age and treachery will overcome youth and skill." "From those who have been given much, much will be expected"
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/