[R] Strange behavior of is.na() on lists

Gabor Grothendieck ggrothendieck at myway.com
Sat Sep 25 19:33:02 CEST 2004


Adrian Alexa <adrian.alexa at gmail.com> writes:
> 
> I have observed that is.na() behaves strange on some lists. Here is a
> simple example:
> 
> 
> > a = list(list('asd'))
> > a
> [[1]]
> [[1]][[1]]
> [1] "asd"
> 
> 
> > for(i in 1:5)
> + print(is.na(a))
> [1] TRUE
> [1] FALSE
> [1] TRUE
> [1] TRUE
> [1] TRUE
> >
> 
> [...snip...]
> 
> In my view this seems to be a bug. If is not, can somebody explain me what is
> really happening and how can I overcome this behavior?

This looks like a bug to me.  As far as a workaround goes you
could try this:


is.na <- function(x)
   if (length(x) == 0) {
      mode(x) <- "logical"
      x
   } else if (inherits(x, "list")) {
      lapply(x, function(i) if (is.atomic(x)) base::is.na(x) else FALSE)
   } else 
      base::is.na(x)




More information about the R-help mailing list