[Rd] 2 Bugs in na.omit.default() (PR#641)

jens.oehlschlaegel@bbdo-interone.de jens.oehlschlaegel@bbdo-interone.de
Wed, 23 Aug 2000 11:48:06 +0200 (MET DST)


# BUG 1: returns empty vector if no NAs present

> nona <- 1:10
> na.omit(nona)
numeric(0)

na.omit.default() has two lines where
  object[-omit]
is used, where omit represents positions of NAs and thus without NAs
  omit = numeric(0)
and since
  -numeric(0) = numeric(0)

  object[-omit] = numeric(0)

It looks like an earlier version of na.omit.default had 'omit' defined as
logical,
at least the last section
  if (any(omit))
seems to expect omit as logical
which leads to

# BUG 2: wrong indication of dropped elements
> withna <- c(NA, 1:10, NA)
> unclass(na.omit(withna))
 [1]  1  2  3  4  5  6  7  8  9 10
attr(,"na.action")
[1]  1 NA
attr(,"na.action")attr(,"class")
[1] "omit"


# suggested fix, experts PLEASE CROSS CHECK
na.omit.default <-
function (object)
{
    if (!is.atomic(object))
        return(object)
    d <- dim(object)
    if (length(d) > 2)
        return(object)
    if (length(d)) {
        omit <- seq(along = object)[is.na(object)]
        hasomit <- length(omit)>0
        if (hasomit){
          omit <- unique(((omit - 1)%%d[1]) + 1)
          nm <- rownames(object)
          object <- object[-omit, , drop = FALSE]
        }
    }
    else {
        omit <- seq(along = object)[is.na(object)]
        hasomit <- length(omit)>0
        if (hasomit){
          nm <- names(object)
          object <- object[-omit]
        }
    }
    if (hasomit) {
        names(omit) <- nm[omit]
        attr(omit, "class") <- "omit"
        attr(object, "na.action") <- omit
    }
    object
}







> version
         _
platform Windows
arch     x86
os       Win32
system   x86, Win32
status
major    1
minor    1.1
year     2000
month    August
day      15
language R



--
Dr. Jens Oehlschlägel
Analyse
BBDO InterOne
Grünstr. 15
40212 Düsseldorf

Tel.: +49 (0)211 1379-187
Fax.: +49 (0)211 1379-461
http://www.bbdo-interone.de


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel 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-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._