[Rd] table( , exclude=NULL) for factor objects

Martin Maechler maechler at stat.math.ethz.ch
Sun Jun 8 00:24:49 MEST 2003


>>>>> "kjetil" == kjetil brinchmann halvorsen <kjetil at entelnet.bo>
>>>>>     on Sat, 07 Jun 2003 11:11:37 -0400 writes:

    kjetil> The NEWS file for R-1.7.0 says:
    kjetil> table() now allows exclude= with factor arguments (requested by
    kjetil> Michael Friendly).

    kjetil> But in a recent R-1.7.1beta:

    >> test <- c(1,2,3,4,5,1,2,3,4,5,NA,NA,1,2,3,4,5)
    >> table(test)
    kjetil> test
    kjetil> 1 2 3 4 5 
    kjetil> 3 3 3 3 3 
    >> table(test, exclude=NULL)
    kjetil> test
    kjetil> 1    2    3    4    5 <NA> 
    kjetil> 3    3    3    3    3    2 
    >> table( as.factor(test), exclude=NULL)

    kjetil> 1 2 3 4 5 
    kjetil> 3 3 3 3 3 


    kjetil> so this seems not to work?

    kjetil> What is strange is that this seems to work in 
    kjetil> example(table).

It's actually not strange, but all consistent and according to help(table):

1) table() does show NA's only when they are factor levels, not
   when they are missing since that's what it's description says:
   it counts factor *levels*:

2) The `exclude' argument in table() is only used when table()'s
   arguments are not yet factors, i.e., not in this case.

> (ft <- as.factor(test))  # clearly, the <NA>'s are missing, not an extra level
 [1] 1    2    3    4    5    1    2    3    4    5    <NA> <NA> 1    2    3   
[16] 4    5   
Levels: 1 2 3 4 5
> ##-> Hence:
> table(ft, exclude=NULL)
ft
1 2 3 4 5 
3 3 3 3 3 
> ## But,
> table(ftN <- factor(test, exclude=NULL))

   1    2    3    4    5 <NA> 
   3    3    3    3    3    2 
> ## or, ``even more'' (giving the same):
> table(ftN, exclude=NULL)
ftN
   1    2    3    4    5 <NA> 
   3    3    3    3    3    2 

Martin Maechler <maechler at stat.math.ethz.ch>	http://stat.ethz.ch/~maechler/
Seminar fuer Statistik, ETH-Zentrum  LEO C16	Leonhardstr. 27
ETH (Federal Inst. Technology)	8092 Zurich	SWITZERLAND
phone: x-41-1-632-3408		fax: ...-1228			<><



More information about the R-devel mailing list