[R] Re: Excluding levels in table and xtabs

Thomas Lumley tlumley at u.washington.edu
Thu Dec 12 22:39:03 CET 2002


On Thu, 12 Dec 2002, Michael Friendly wrote:

> Having looked over the replies and examined the code, I can't
> see any reason for table (and xtabs) to avoid honoring the
> exclude= argument for factors.  There are often reasons for wanting
> to exclude certain levels, even non-missing in making a table.
>
> In my application, John Fox suggested that I could circumvent
> the problem by reading in the .csv file with na.strings="".
> However, it was only for making tables that I wanted to exclude
> the "" categories.
>
> The change to table() to have it honor the exclude option for
> factors is quite straight-forward.  I wonder if the R team
> will consider placing this on its list.  (revised version below)
>

It doesn't work for me (R1.6.1)

R> a<-factor( rep(c("A","B","C"),10))
R> a
 [1] A B C A B C A B C A B C A B C A B C A B C A B C A B C A B C
Levels: A B C
> mftable(a)
a
 A  B  C
10 10 10
> mftable(a,exclude=c("A",NA))
a
 A  B  C
10 10 10
Warning message:
NAs introduced by coercion


and in addition it loses empty levels

> levels(a)<-LETTERS[1:5]
> table(a)
a
 A  B  C  D  E
10 10 10  0  0
> mftable(a)
a
 A  B  C
10 10 10


I agree that the fix needs to be done, though.



	-thomas




More information about the R-help mailing list