[R] confusion on levels() function, and how to assign a wanted order to factor levels, intentionally?

2009-06-16 Thread Mao Jianfeng
Dear R-helpers, I want to make a series of boxplots on several numeric univariates with two group variables (species and population, population nested in species, and with population as the X-axis). In order to get a proper order of the individual populations in X-axis, I need to assign a wanted

Re: [R] confusion on levels() function, and how to assign a wanted order to factor levels, intentionally?

2009-06-16 Thread xavier . chardon
Hi, The way you do it actually renames the factors one after each other (it replaces the values in the data frame, which is not what you want). Have a look at this code: test - data.frame(id=c(1,2,3), fac=c(lv1, lv2, lv3) ) levels(test$fac) test$fac2 - factor(test$fac, levels=c(lv3, lv2, lv1))

Re: [R] confusion on levels() function, and how to assign a wanted order to factor levels, intentionally?

2009-06-16 Thread Zeljko Vrba
On Tue, Jun 16, 2009 at 03:14:01PM +0800, Mao Jianfeng wrote: levels(d$population)-c(YXPy01, KMPy01, YLPy01, GSPy02, BCPy01, LJPy01, GYPt01, YLPd01, CYPd01, CYPd02, CYPd03, BXPd01, NSPt01) I'm not at home with factors myself, but maybe this will do the trick for you: d$population -

Re: [R] confusion on levels() function, and how to assign a wanted order to factor levels, intentionally?

2009-06-16 Thread Mark Difford
Hi Mao, I am confused. And, I want to know how to assign a wanted order to factor levels, intentionally? You want ?relevel. Although the documentation leads one to think that it can only be used to set a reference level, with the other levels being moved down, presently it can in fact be

Re: [R] confusion on levels() function, and how to assign a wanted order to factor levels, intentionally?

2009-06-16 Thread baptiste auguie
Hi, I tend to use a slightly modified version of stats::relevel, (from an old thread on this list), relevel = function (x, ref, ...) { lev - levels(x) if (is.character(ref)) ref - match(ref, lev) if (any(is.na(ref))) stop('ref' must be an existing level) nlev - length(lev)

Re: [R] confusion on levels() function, and how to assign a wanted order to factor levels, intentionally?

2009-06-16 Thread Peter Dalgaard
Mark Difford wrote: Hi Mao, I am confused. And, I want to know how to assign a wanted order to factor levels, intentionally? You want ?relevel. Although the documentation leads one to think that it can only be used to set a reference level, with the other levels being moved down,

Re: [R] confusion on levels() function, and how to assign a wanted order to factor levels, intentionally?

2009-06-16 Thread baptiste auguie
Commenting on this, is there a strong argument against modifying relevel() to reorder more than one level at a time? I started a topic a while back (recursive relevel, https://stat.ethz.ch/pipermail/r-help/2009-January/184397.html) and I've happily used the proposed change since then by