[R] including NAs in a table

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Tue Nov 16 10:17:42 CET 1999


Robert.King at mailbox.gu.edu.au writes:

> I have two factors, WC and WD.  If I want a table including all the values
> of each factor, including NAs, I can get a table by going:
> 
> table(as.character(WC),as.character(WD),exclude=c())
> 
> Is there an easier way?
> 
> I guess this is a feature request for table()

It's a bit tricky:

table() has this internally

        if (is.factor(a)) 
            cat <- a
        else cat <- factor(a, exclude = exclude)

Now if you do factor(a, exclude = exclude) unconditionally, and have
exclude=NULL,  you'll get the NA's in the table, but lose all levels
not present in data, which can also be a pain...

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help 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-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list