[R] NA in table with integer types

Prof Brian Ripley
2005-04-08T07:29:37Z

NaN only applies to double values: there is no integer NaN (nor Inf nor
-Inf).  The difference is clear from

> factor(x, exclude=NaN)
[1] 1    2    3    3    <NA>
Levels: 1 2 3 <NA>
> factor(as.integer(x), exclude=NaN)
[1] 1    2    3    3    <NA>
Levels: 1 2 3

If you read ?factor it says

  exclude: a vector of values to be excluded when forming the set of
           levels. This should be of the same type as 'x', and will be
           coerced if necessary.

and as.integer(NaN) is integer NA.  So  factor(as.integer(x), exclude=NaN)
is the same as  factor(as.integer(x), exclude=NA).

On Thu, 7 Apr 2005, Paul Rathouz wrote:

>
> Hi -- I am having the following problem with table() when applied to
> vectors of type (mode) integer.  When I use the table() command, I can
> *only obtain an entry in the table for NA values by using exclude=NULL*.
> Just issuing exclude=NaN will not do it.  See below, where x is double at
> first, and then coerced to integer and notice the difference.  Is this a
> bug or is there something that I do not understand about the integer data
> type?  That is, is there some other value besides NA and NaN that
"missing
> integers" take? Thanks -- pr
>
> ------------------------------
>> x <- c(1,2,3,3,NA)
>> is.double(x)
> [1] TRUE
>> table(x,exclude=NA)
> x
> 1 2 3
> 1 1 2
>> table(x,exclude=NaN)
> x
>   1    2    3 <NA>
>   1    1    2    1
>> table(x,exclude=NULL)
> x
>   1    2    3 <NA>
>   1    1    2    1
>>
>> x <- as.integer(x)
>> x
> [1]  1  2  3  3 NA
>> is.na(x)
> [1] FALSE FALSE FALSE FALSE  TRUE
>> is.integer(x)
> [1] TRUE
>> table(x,exclude=NA)
> x
> 1 2 3
> 1 1 2
>> table(x,exclude=NaN)
> x
> 1 2 3
> 1 1 2
>> table(x,exclude=NULL)
> x
>   1    2    3 <NA>
>   1    1    2    1

--
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595