Skip to content

how to include 'NA's in xtabs?

6 messages · Kosenkov Kirill, Sundar Dorai-Raj, PIKAL Petr +1 more

#
Hello!

I have a dataset with NA's in some variables (factors), for example:
$ P67 : Factor w/ 2 levels "-","+": NA 2 1 NA NA 2 1 1 2 NA ...

I need to use 'xtabs' like
xtabs(~x$P67)
It works well and produces something like this:
x$P67
    -    +
  779 1318
but i want to compute NA's too, like this:
x$P67
    -    +   NA
  779 1318  137

I am trying xtabs(~x$P67, exclude=NULL) but xtabs does not compute 
'NA's in this case too.

I do not want to transform my data (do not want to do any 
substitution on NA's).

How i can say 'xtabs' to compute NA-values?

and second question: how to use argument 'na.action' in 'xtabs'?
'xtabs' help page does not explain this.

Thanks!
#
Kosenkov Kirill wrote:
The key is here. Should be 3 levels if you want NAs included.
Try this:

x$P67 = factor(x$P67, exclude = NULL)
xtabs(~x$P67)


Here's an example:
R> x = factor(c(1:2, NA), exclude = NULL, labels = c("+", "-", "0"))
R> xtabs(~ x)
x
+ - 0
1 1 1

Sundar
#
Thanks for your reply, but i dont want to transform my data
with 'factor' or anything else. I really need 'NA's as NA's
and i dont need them as levels of factor.

I just seeking possibility to compute count of 'NA's with
'xtabs' without transforming my data. Maybe 'na.action' argument
in 'xtabs' may help? But i dont know how to use it.

Any suggestions?
Sundar Dorai-Raj wrote:

            
#
Hi
On 15 May 2003 at 0:18, Kosenkov Kirill wrote:

            
what about using summary instead of xtabs
159  160 NA's 
   5   22    3
ostbet$delka
159 160 
  5  22
Cheers
Petr Pikal
petr.pikal at precheza.cz
p.pik at volny.cz
#
That's ok if i do something like xtabs(~f1). Thanks.
What about xtabs(w~f1+f2)? If i need to do crosstabulation
and factor variables contains NA's?

I want to repeat my second question: how to use 'na.action'
argument in xtabs? Can anybody explain, how is it works?
Petr Pikal wrote:

            
#
On 15 May 2003 at 22:34, Kosenkov Kirill wrote:
The problem with xtabs discussed in this thread is the same problem 
as was discussed for table in R1.6.2 time. The NEWS file for R1.7.0 
has:

 o	table() now allows exclude= with factor arguments (requested by
	Michael Friendly).

but (rw1070, windows XP):
[1]  1  2  3  1  2  3 NA NA  1  2  3
test
1 2 3 
3 3 3
test
   1    2    3 <NA> 
   3    3    3    2
test
1 2 3 
3 3 3
test
1 2 3 
3 3 3 

so the announced fix does'nt seem to work?

I thought the same fix could be applied to xtabs, but first rw1070
must live up to its NEWS file.

Kjetil Halvorsen