[R] Odp: regrouping factor levels
petr.pikal at precheza.cz
Mon May 25 14:48:16 CEST 2009
r-help-bounces at r-project.org napsal dne 22.05.2009 18:53:37:
> Hi all,
> I had some trouble in regrouping factor levels for a variable. After
> experiments, I have figured out how I can recode to modify the factor
> I would now like some help to understand why some methods work and
> Here's my code :
> ###some trials in recoding factor levels
> ##first method of recoding factors
> ##second method
> 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)
> #third method
> I first tried method 1 and had no luck with it at all. The levels A, B,
> just got added to the existing levels without affecting the fac
> 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
See the difference in those 2 selection methods
a, b, c are vector items not names so you need to check their presence by
which.levels <- levels(fac1) %in% c("a","b","c")
levels(fac1)[which.levels] <- "A"
levels(fac4)<-c(rep("A",3), rep("B", 3), rep("C",4))
You can either to replace all levels at once or to select levels and
replace them with correct number of items.
> In my application, I had long factor names and Tinn-R just would not
> accept statements running to several lines. Partial substitution was
> then. Having spent a considerable amount of time on this, I would like
> understand the underlying problem with method 1 as it is. The deeper
> understanding could be useful for me later.
> Thanking You,
> R-help at r-project.org mailing list
> PLEASE do read the posting guide
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help