[Rd] table on numeric vector with exclude argument containing value missing from vector causes warning + "NaN" levels incorrectly removed from factors

Karl Ove Hufthammer karl at huftis.org
Fri Jan 21 11:30:58 CET 2011

I *think* the following may be considered a bug or two, but would appreciate 
any comments before (not) filing an official bug report.

Possible bug 1: ‘table’ on numeric vector with ‘exclude’ argument containing 
value missing from vector causes warning
Possible bug 2: ‘table’ incorrectly tries to remove "NaN" levels

The help page for ‘table’ says the the first argument is ‘one or more 
objects which can be interpreted as factors (including character strings) 
[…]’. Does this include numeric vectors? Numeric vectors seems to work fine. 

  x = sample(1:3, 100, replace=TRUE)

The ‘exclude’ argument explicitly mentions factor levels, but seems to work 
fine for other objects too. Example:

  table(x, exclude=2)

It’s actually not clear from the help page what is meant by ‘levels to 
remove from all factors in ...’, but it seems like a character vector is 
expected. And indeed the following also works:

  table(x, exclude="2")

However, setting the ‘exclude’ argument to a value not contained in 
the vector to be tabulated,

  table(x, exclude="foo")

causes the following warning:

  In as.vector(exclude, typeof(x)) : NAs introduced by coercion’:

The correct results is produced, though. Note that all of the following does 
*not* cause any warning:

  table(x, exclude=NA)
  table(x, exclude=NaN)
  table(factor(x), exclude="foo")
  table(as.character(x), exclude="foo")

I also wonder about the inclusion of ‘NaN’ in the definition of ‘table’:

table(..., exclude = if (useNA == "no") c(NA, NaN), useNA = c("no", 
    "ifany", "always"), dnn = list.names(...), deparse.level = 1) 

A factor can’t include a NaN level, as the levels values are always
strings or NA. And having the above definition causes "NaN" (string)
levels to mysteriously disappear when run through ‘table’. Example:

  table(factor(c("NA",NA,"NcN","NbN", "NaN")))


   NA NbN NcN 
    1   1   1

(The missing NA is not a bug; it’s caused by useNA="no".)

R version 2.12.1 Patched (2011-01-20 r54056)
Platform: i686-pc-linux-gnu (32-bit)

[1] C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base   

Karl Ove Hufthammer

More information about the R-devel mailing list