[R] missing -> nonmissing levels

Jon Minton gsp05jm at sheffield.ac.uk
Fri Feb 16 23:40:59 CET 2007



-----Original Message-----
From: Prof Brian Ripley [mailto:ripley at stats.ox.ac.uk] 
Sent: 16 February 2007 18:40
To: Jon Minton
Cc: r-help at stat.math.ethz.ch; 'Jon Minton'
Subject: Re: [R] missing -> nonmissing levels

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

-- 
No virus found in this incoming message.


17:40
 

-- 



17:40



More information about the R-help mailing list