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. Best wishes, Ant?nio Olinto Fisheries Institute Sao Paulo - BRAZIL www.institutopesca.sp.gov.br -------------- next part -------------- An HTML attachment was scrubbed... URL: https://stat.ethz.ch/pipermail/r-help/attachments/20010628/af302a82/attachment.html
changing values
4 messages · Antonio Olinto, Brian Ripley, Philippe GROSJEAN
On Thu, 28 Jun 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)
levels(x) <- c(1,1,2,2) x
[1] 1 1 1 1 2 1 2 2 Levels: 1 2 is the simplest answer.
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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Thanks very much Dr. Ripley. After thinking a little more on my question I decide to use "ifelse" x <- c(x) x1 <- ifelse (x <=2, 1, x) x2 <- ifelse (x1>=3, 2, x1) x <- factor(x2) it works, but "levels(x) <- c(1,1,2,2)" is too much easier. Thanks a lot. Ant?nio Olinto ----- Original Message ----- From: "Prof Brian Ripley" <ripley at stats.ox.ac.uk> To: "Antonio Olinto" <aolinto at bignet.com.br> Cc: <r-help at stat.math.ethz.ch> Sent: Thursday, June 28, 2001 11:34 AM Subject: Re: [R] changing values
On Thu, 28 Jun 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)
levels(x) <- c(1,1,2,2) x
[1] 1 1 1 1 2 1 2 2 Levels: 1 2 is the simplest answer. -- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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