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

David Winsemius dwinsemius at comcast.net
Sat Sep 5 04:03:50 CEST 2009


On Sep 4, 2009, at 7:49 PM, ws wrote:

> Hi all,
>
> I cannot get xtabs to count NA's like I expect.  Below is a sample  
> session, and
> note that the last two calls to xtabs() yield exactly the same thing.
>
> I am running R-2.5.0 -- if there was a bug in xtabs that got fixed,  
> I would love
> to know about it. If there is a bug tracker somewhere, I can file it  
> too. It
> isn't my fault that the server is so ridiculously outdated, but I  
> would have to
> install a more recent version in my home directory to fix this, so I  
> don't
> really want to.
>
> Note also that I know various approaches to working around the  
> problem, so I
> don't need help with that, just with whether or not xtabs works with  
> nas or not.
>
> TIA
>
> $ x=as.data.frame(list(wkhp=WKHP[1:20]))
> $ x
>   wkhp
> 1    NA
> 2    NA
> 3    60
> 4    NA
> 5    20
> 6    40
> 7    30
> 8    40
> 9    40
> 10   40
> 11   45
> 12   40
> 13   40
> 14   NA
> 15   40
> 16   40
> 17   40
> 18   60
> 19   60
> 20   40
> $ xtabs(~wkhp, x)
> wkhp
> 20 30 40 45 60
> 1  1 10  1  3
> $ xtabs(~wkhp, x, exclude=NULL)  # Look -- no counts of NAs!
> wkhp
> 20 30 40 45 60
> 1  1 10  1  3

 > xtabs(~wkhp, x, exclude=NULL, na.action=na.pass)
wkhp
   20   30   40   45   60 <NA>
    1    1   10    1    3    4

-- 
David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list