[R] regrouping factor levels
rv15i at yahoo.se
Fri May 22 18:53:37 CEST 2009
I had some trouble in regrouping factor levels for a variable. After some experiments, I have figured out how I can recode to modify the factor levels. I would now like some help to understand why some methods work and others don't.
Here's my code :
###some trials in recoding factor levels
##first method of recoding factors
levels(fac2)[c(2,3,4)]<-"B" # not c(4,5,6)
levels(fac2)[c(3,4,5,6)]<-"C" # not c(7,8,9,10)
I first tried method 1 and had no luck with it at all. The levels A, B, and C just got added to the existing levels without affecting the fac variable.
After some time, I was able to figure out how I should use method 2.
After reading the help documentation, I arrived at method 3.
I would appreciate help in understanding why the first method does not work. In my application, I had long factor names and Tinn-R just would not accept statements running to several lines. Partial substitution was desirable then. Having spent a considerable amount of time on this, I would like to understand the underlying problem with method 1 as it is. The deeper understanding could be useful for me later.
More information about the R-help