> x <- factor(1:3, labels = c("b" , "f", "minus")) > x[1] b f minus Levels: b f minus I want to change all "minus" to "b". I know that the simplest way to do this is> levels(x) <- c("b", "f", "b")and also that> x[x == "minus"] <- "b" > x <- factor(x)works. But why not> x <- ifelse(x == "minus", "b", x) > x <- factor(x)x [1] 1 2 b Levels: 1 2 b ? -- G?ran Brostr?m
The problem is that ifelse strips attributes. For example, consider this which has nothing to do with factors but illustrates the point with ifelse:> x <- ts(1:12) > y <- ifelse(TRUE, x, x) > str(x)Time-Series [1:12] from 1 to 12: 1 2 3 4 5 6 7 8 9 10 ...> str(y)int 1 On 3/17/06, G?ran Brostr?m <goran.brostrom at gmail.com> wrote:> > x <- factor(1:3, labels = c("b" , "f", "minus")) > > x > [1] b f minus > Levels: b f minus > > I want to change all "minus" to "b". I know that the simplest way to do this is > > > levels(x) <- c("b", "f", "b") > > and also that > > > x[x == "minus"] <- "b" > > x <- factor(x) > > works. But why not > > > x <- ifelse(x == "minus", "b", x) > > x <- factor(x) > x > [1] 1 2 b > Levels: 1 2 b > > ? > -- > G?ran Brostr?m > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Sorry, here is the code:> x <- ts(1:12) > y <- ifelse(rep(TRUE, 12), x, x) > str(x)Time-Series [1:12] from 1 to 12: 1 2 3 4 5 6 7 8 9 10 ...> str(y)int [1:12] 1 2 3 4 5 6 7 8 9 10 ... On 3/17/06, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:> The problem is that ifelse strips attributes. For example, consider > this which has nothing to do with factors but illustrates the point > with ifelse: > > > x <- ts(1:12) > > y <- ifelse(TRUE, x, x) > > str(x) > Time-Series [1:12] from 1 to 12: 1 2 3 4 5 6 7 8 9 10 ... > > str(y) > int 1 > > > On 3/17/06, G?ran Brostr?m <goran.brostrom at gmail.com> wrote: > > > x <- factor(1:3, labels = c("b" , "f", "minus")) > > > x > > [1] b f minus > > Levels: b f minus > > > > I want to change all "minus" to "b". I know that the simplest way to do this is > > > > > levels(x) <- c("b", "f", "b") > > > > and also that > > > > > x[x == "minus"] <- "b" > > > x <- factor(x) > > > > works. But why not > > > > > x <- ifelse(x == "minus", "b", x) > > > x <- factor(x) > > x > > [1] 1 2 b > > Levels: 1 2 b > > > > ? > > -- > > G?ran Brostr?m > > > > ______________________________________________ > > R-help at stat.math.ethz.ch mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
>> x <- factor(1:3, labels = c("b" , "f", "minus")) >> x > [1] b f minus > Levels: b f minus > > I want to change all "minus" to "b". I know that the simplest way to do this is > >> levels(x) <- c("b", "f", "b") > > and also that > >> x[x == "minus"] <- "b" >> x <- factor(x) > > works. But why not > >> x <- ifelse(x == "minus", "b", x) >> x <- factor(x) > x > [1] 1 2 b > Levels: 1 2 b >I find particulary usefull the list approach. Here is the example from levels help page. I like this approach, since you can modify list and apply it to a factor via levels. ## we can add levels this way: f <- factor(c("a","b")) levels(f) <- c("c", "a", "b") f f <- factor(c("a","b")) levels(f) <- list(C="C", A="a", B="b") f I was playing around this last week and wrote a simple function[1], which can save you some work in getting such a list from a factor. Is R core interested in it? [1]http://www.bfro.uni-lj.si/MR/ggorjan/software/R/ggmisc/factorMap.R -- Lep pozdrav / With regards, Gregor Gorjanc ---------------------------------------------------------------------- University of Ljubljana PhD student Biotechnical Faculty Zootechnical Department URI: http://www.bfro.uni-lj.si/MR/ggorjan Groblje 3 mail: gregor.gorjanc <at> bfro.uni-lj.si SI-1230 Domzale tel: +386 (0)1 72 17 861 Slovenia, Europe fax: +386 (0)1 72 17 888 ---------------------------------------------------------------------- "One must learn by doing the thing; for though you think you know it, you have no certainty until you try." Sophocles ~ 450 B.C.