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

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to