[R] Reassign Multiple Factors to same Factor Value

Lorenzo Isella lorenzo.isella at gmail.com
Mon Mar 25 23:18:26 CET 2013


Dear All,
Probably something very easy, but I am looking for the most efficient ways  
to achieve this.
Consider the following snippet

y<-c('a','b','c','d','e','f','g')
x<-rnorm(length(y))
df<-data.frame(y,x)

leading to

> df$y
[1] a b c d e f g
Levels: a b c d e f g

Now, I would like to replace levels ('e','f','g') of df$y with a new level  
'other' so that levels(df$y) returns

a b c d other

What is the easiest way to achieve this, considering that

df$y[5:7]<-'other'
Does not work?
Cheers

Lorenzo



More information about the R-help mailing list