From maechler at stat.math.ethz.ch  Sat Sep 10 19:49:03 2016
From: maechler at stat.math.ethz.ch (Martin Maechler)
Date: Sat, 10 Sep 2016 19:49:03 +0200
Subject: [Rd] table(exclude = NULL) always includes NA
In-Reply-To: <485115119.1895185.1473475014687@mail.yahoo.com>
References: <485115119.1895185.1473475014687.ref@mail.yahoo.com>
	<485115119.1895185.1473475014687@mail.yahoo.com>
Message-ID: <22484.18319.99371.446681@stat.math.ethz.ch>

>>>>> Suharto Anggono Suharto Anggono <suharto_anggono at yahoo.com>
>>>>>     on Sat, 10 Sep 2016 02:36:54 +0000 writes:

    > Looking at the code of function 'table' in R devel r71227, I see that the part "remove NA level if it was added only for excluded in factor(a, exclude=.)" is not quite right.
    > In
    > is.na(a) <- match(a0, c(exclude,NA), nomatch=0L)   ,
    > I think that what is intended is
    > a[a0 %in% c(exclude,NA)] <- NA  .
yes.
    > So, it should be
    >   is.na(a) <- match(a0, c(exclude,NA), nomatch=0L) > 0L
    > or
    >   is.na(a) <- as.logical(match(a0, c(exclude,NA), nomatch=0L))  .
    > The parallel code
    >    is.na(a) <- match(a0,   exclude,     nomatch=0L)
    > is to be treated similarly.

indeed.  I may have been  very wrongly thinking that `is.na<-`
coerced its value to logical... or otherwise not thinking at all ;-)


    > Example that gives wrong result in R devel r71225:
    > table(3:1, exclude = 1)
    > table(3:1, exclude = 1, useNA = "always")
    > --------------------------------------------

Thanks a lot, Suharto.   You are entirely correct.

I'm amazed that  table(*, exclude = *)  seems so rarely used / tested,
that this has gone undetected for almost four weeks.
It is fixed now with svn r71230.

Martin



[Rd] table(exclude = NULL) always includes NA
Martin Maechler