Skip to content

argument 'exclude' in function xtabs

3 messages · Erik Iverson, Matthieu Lesnoff

#
I think I see what's happening, and it's a little confusing to me, too.  
If your classifying factor to xtabs is not
actually a factor, but say, a character vector, it will be converted to a
factor
while using the "exclude" argument.  However, if it already is a factor,
this does not happen.
So, for example, contrast:

trt.char <- c("A","B","C","A","B","C")
xtabs(~trt.char, exclude = "B")

with

trt.fac <- factor(c("A","B","C","A","B","C"))
xtabs(~trt.fac, exclude = "B")

Hope that helps,
Erik Iverson
1 day later
#
Ok thanks Erik

Indeed, "exclude" in "xtabs" seems working whith character vectors:
[1] "A" "B" "C" "A" "B" "C"
x
A C 
2 2 

and not directly with factors:
[1] A B C A B C
Levels: A B C
[1] "A" "B" "C"
x
A B C 
2 2 2 

while function "table" does it:
[1] A B C A B C
Levels: A B C
x
A C 
2 2 

However, one point remains confusing for me in function "table". When I add
a NA in the vector, it still works:
[1] A    B    C    A    B    C    <NA>
Levels: A B C
x
A C 
2 2 

but not anymore when I use argument "exclude = NULL" in function "factor":
[1] A    B    C    A    B    C    <NA>
Levels: A B C <NA>
x
   A    B    C <NA> 
   2    2    2    1 

Finally, if I remove NA, it works again:
[1] A B C A B C
Levels: A B C
x
A C 
2 2 

it looks like "exclude" does not work when NA is a level




Regards

Matthieu