[R] NA in table with integer types

Paul Rathouz prathouz at health.bsd.uchicago.edu
Fri Apr 8 15:05:56 CEST 2005


OK.  Thanks.  So, if you use table() on a factor that contains NA's, but
for which NA is not a level, is there any way to get table to generate an
entry for the NAs?  For example, in below, even "exclude=NULL" will not
give me an entry for <NA> on the factor y:

> x <- c(1,2,3,3,NA)
> y <- factor(x)
> y
[1] 1    2    3    3    <NA>
Levels: 1 2 3
> table(y,exclude=NA)
y
1 2 3
1 1 2
> table(y,exclude=NaN)
y
1 2 3
1 1 2
> table(y,exclude=NULL)
y
1 2 3
1 1 2

On Fri, 8 Apr 2005, Prof Brian Ripley wrote:

> 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).
>

[rest deleted]

==========================================================================
Paul Rathouz, Assoc. Professor       ph   773-834-1970
Dept. of Health Studies, Rm. W-264   fax  773-702-1979
University of Chicago                prathouz at health.bsd.uchicago.edu
5841 S. Maryland Ave. MC 2007
Chicago, IL  60637




More information about the R-help mailing list