[R] changing values

Philippe Grosjean phgrosje at ulb.ac.be
Thu Jun 28 18:53:15 CEST 2001


Antonio Olinto wrote:

>Dear R list members,
>I have a factor vector with levels 1, 2, 3 and 4. I want to re-group it: 1
and 2 will be 1 and 3 and 4 will be 2, e.g.:
>x <- c(1, 1, 1, 2, 3, 2, 3, 4, ... )
>x <- factor(x)
>What I have to do create a new vector where 1 1 1 2 3 2 3 4 ... is
transformed in 1 1 1 1 2 1 2 2 ... ? (I have more than 200 lines in my
dataframe)
>Thanks in advance.

I don't know if it is the best way to do it, but it works:

x <- as.numeric(x)
x[x<=2] <- 1
x[x>2] <- 2
x <- as.factor(x)

Best regards,

Philippe Grosjean



...........]<(({°<...............<°}))><...............................
 ) ) ) ) )       __                      __
( ( ( ( (       |__)                    |  _
 ) ) ) ) )      |   hilippe             |__)rosjean
( ( ( ( (       Marine Biol. Lab., ULB, Belgium
 ) ) ) ) )                               __
( ( ( ( (       |\  /|                  |__)
 ) ) ) ) )      | \/ |ariculture &      |__)iostatistic
( ( ( ( (
 ) ) ) ) )      e-mail: phgrosje at ulb.ac.be
( ( ( ( (       SciViews project coordinator (http://www.sciviews.org)
 ) ) ) ) )
.......................................................................

-------------- next part --------------
An HTML attachment was scrubbed...
URL: https://stat.ethz.ch/pipermail/r-help/attachments/20010628/12a718a7/attachment.html


More information about the R-help mailing list