[R] How to change the levels of a variable???

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Sep 21 14:01:17 CEST 2001


On Fri, 21 Sep 2001, Ronaldo Reis Jr. wrote:

> Hi,
>
> I have a simple question.
>
> I have a variable with 3 levels.
>
> Variable
> A A A A A A
> B B B B B B
> C C C C C C
>
> I need to make a new vector renaming de levels B and C for D.
>
> NewVariable
> A A A A A A
> D D D D D D
> D D D D D D
>
> I dont find how to make it in manuals or in this list.

See ?levels and follow the link to ?"levels<-.factor"

> old <- factor(LETTERS[1:3])
> old
[1] A B C
Levels:  A B C
> levels(new) <- c("A", "D", "D")
> new
[1] A D D
Levels:  A D

You could use
> factor(old, labels=c("A", "D", "D"))
[1] A D D
Levels:  A D D

but as you see, that is not precisely the same thing.

-- 
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 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list