[R] missing -> nonmissing levels

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Feb 16 19:39:58 CET 2007


Well, if it is missing, how do you know what level to turn it back into?
That is what NA means in R: not available.

If you want missings to be a separate level, you could use

> factor(as.character(X), exclude=NULL)
[1] A    <NA> B
Levels: A B <NA>

BTW, using summary() for a length-3 object is not helpful, and please send 
properly formatted text emails as the posting guide does ask of you.

On Fri, 16 Feb 2007, Jon Minton wrote:

> Hi,
>
> I expect this is simple but haven’t found an answer looking on the
> archives...
>
>
>
> I want to convert ‘NA’ (missing) to particular levels (nonmissing) in factor
> vectors.
>
>
>
> e.g. I know
>
>> X <- c(1, 2, 3)
>
>> summary(X)
>
>   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
>
>    1.0     1.5     2.0     2.0     2.5     3.0
>
>> X <- as.factor(X)
>
>> summary(X)
>
> 1 2 3
>
> 1 1 1
>
>> levels(X)
>
> [1] "1" "2" "3"
>
>> levels(X) <- c("A", NA, "B")
>
>> summary(X)
>
>   A    B NA's
>
>   1    1    1
>
>
>
> But what if I want to turn the NA back into a level?
>
> How do I do this?
>
>
>
> Thanks,  Jon
>
>
>
>
>
>
>

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


More information about the R-help mailing list