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

David Winsemius dwinsemius at comcast.net
Thu Sep 24 09:13:15 CEST 2009


On Sep 24, 2009, at 3:06 AM, David Winsemius wrote:

>
> On Sep 23, 2009, at 5:11 PM, ws wrote:
>
>> Webb Sprague <webb.sprague <at> gmail.com> writes:
>>
>>>
>>>>> xtabs(~wkhp, x, exclude=NULL, na.action=na.pass)
>>>> wkhp
>>>> 20   30   40   45   60 <NA>
>>>>  1    1   10    1    3    4
>>
>> now this doesn't even work !!!!
>
> Try:
>
> wtf <- factor(x, levels(c(levels(wtf), NA), exclude=NULL)
> xtabs (~ wtf, exclude=NULL, na.action=na.pass)

Note: that will mean that sum(is.na(wtf)) will return 0 and  
is.na(wtf)<- will no longer have any valid targets.

>
> -- David
>
>
>>> table(wtf, exclude=NULL)
>> wtf
>>  [0,10)   [10,20)   [20,30)   [30,40)   [40,50)   [50,60)    
>> [60,70)   [70,80)
>>    8562     15297      9666      6659      3583       667       
>> 1357       238
>> [80,90)  [90,100) [100,110) [110,120) [120,130) [130,140) [140,150)  
>> [150,160)
>>      61       311        57        19        95         7          
>> 3       111
>> [180,190)      <NA>
>>     161     62161
>>
>> Compare:
>>> xtabs (~ wtf, exclude=NULL, na.action=na.pass)
>> wtf
>>  [0,10)   [10,20)   [20,30)   [30,40)   [40,50)   [50,60)    
>> [60,70)   [70,80)
>>    8562     15297      9666      6659      3583       667       
>> 1357       238
>> [80,90)  [90,100) [100,110) [110,120) [120,130) [130,140) [140,150)  
>> [150,160)
>>      61       311        57        19        95         7          
>> 3       111
>> [180,190)
>>     161
-- 
David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list