R-alpha: boxplot() with NA's

Kurt Hornik (hornik@ci.tuwien.ac.at)
Sat, 29 Mar 1997 10:09:10 +0100


Date: Sat, 29 Mar 1997 10:09:10 +0100
Message-Id: <199703290909.KAA18353@aragorn.ci.tuwien.ac.at>
From: Kurt Hornik <hornik@ci.tuwien.ac.at>
To: r-testers@stat.math.ethz.ch
Subject: R-alpha: boxplot() with NA's

Boxplotting objects containing NA's fails with the error message
	
  Error in plot.window(xlim = c(0.5, n + 0.5), ylim = limits, log = log):
  NAs not allowed in ylim

The reason seems to be that the extremes of the whiskers computed by
boxplot.stats(x) are NA if x contains NA's, because

  out <- x < (stats[2] - coef * iqr) | x > (stats[4] + coef * iqr)

still contains NA's and hence the subsequent

  if (coef > 0) 
    stats[c(1, 5)] <- range(x[!out])

will set the extremes to NA.

An obvious idea would be to start by removing NA's in x ... I am not
sure if this breaks anything.  If this is o.k., the new code for
boxplot.stats could e.g. be

boxplot.stats <- function(x, coef)
{
  x <- x[!is.na(x)]
  n <- length(x)
  stats <- fivenum(x)
  iqr <- diff(stats[c(2, 4)])
  out <- x < (stats[2]-coef*iqr) | x > (stats[4]+coef*iqr)
  if(coef > 0) stats[c(1, 5)] <- range(x[!out])
  conf <- stats[3]+c(-1.58, 1.58)*diff(stats[c(2, 4)])/sqrt(n)
  list(stats=stats, n=n, conf=conf, out=x[out])
}

-k
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
r-testers mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-testers-request@stat.math.ethz.ch
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-