[R] Reassign Multiple Factors to same Factor Value

soon yi soon.yi at ymail.com
Tue Mar 26 00:32:06 CET 2013


or just

levels(df$y)[5:7]<-"others"


Ista Zahn wrote
> Hi Lorenzo,
> 
> On Mon, Mar 25, 2013 at 6:18 PM, Lorenzo Isella
> <

> lorenzo.isella@

> > wrote:
>> 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,
> 
> levels(df$y) <- c("a", "b", "c", "d", rep("others", 3))
> 
> will do the job.
> 
> considering that
>>
>> df$y[5:7]<-'other'
>> Does not work?
> 
>> Cheers
>>
>> Lorenzo
>>
>> ______________________________________________
>> 

> R-help@

>  mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
> 
> ______________________________________________

> R-help@

>  mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.





--
View this message in context: http://r.789695.n4.nabble.com/Reassign-Multiple-Factors-to-same-Factor-Value-tp4662437p4662440.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list