[R] Problem with xtabs(), exclude=NULL, and counting NA's

David Winsemius dwinsemius at comcast.net
Thu Sep 24 16:27:53 CEST 2009


On Sep 24, 2009, at 10:06 AM, hadley wickham wrote:

>> wtf <- factor(x, levels(c(levels(wtf), NA), exclude=NULL)
>> xtabs (~ wtf, exclude=NULL, na.action=na.pass)
>
> Also see addNA.
>

That is nice. The addNA function does not exactly jump off the page  
for the (too) casual reader. In the context of the OP's original  
problem, these lines of code are illustrative:

 > xtabs(~addNA(wkhp), x, exclude=NULL, na.action=na.pass)
addNA(wkhp)
   20   30   40   45   60 <NA>
    1    1   10    1    3    4
 > x$wtf <- cut(x$wkhp, breaks=seq(20, 80, by=20) )
 > xtabs( ~ addNA(wkhp), x)
addNA(wkhp)
   20   30   40   45   60 <NA>
    1    1   10    1    3    4
 > xtabs( ~ addNA(wtf), x)
addNA(wtf)
(20,40] (40,60] (60,80]    <NA>
      11       4       0       5

> Hadley
>
>
> -- 
> http://had.co.nz/

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list