I would like to define the order of the levels of a factor. The relevel function would work but since I have 20 levels I would prefer to declare the order explicitly. Using a smaller example levels(oldfactor) "b1" "b2" "r1" "r2" nufactor <- order(oldfactor,order=c("b1","r1","b2","r2")) # my fabricated function levels(nufactor) "b1" "r1" "b2" "r2" Thanks in advance for any help. Regards Ross Darnell -- Email r.darnell at shrs.uq.edu.au -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Ross Darnell <r.darnell at shrs.uq.edu.au> writes:> I would like to define the order of the levels of a factor. > > The relevel function would work but since I have 20 levels I would prefer > to declare the order explicitly. Using a smaller example > > levels(oldfactor) > "b1" "b2" "r1" "r2" > nufactor <- order(oldfactor,order=c("b1","r1","b2","r2")) # my fabricated function > levels(nufactor) > "b1" "r1" "b2" "r2" > > Thanks in advance for any help.e.g.:> f <- factor(letters[1:3]) > g <- factor(f,levels=rev(levels(f))) # or levels(f)[c(3,1,2)], etc. > g[1] a b c Levels: c b a> f[1] a b c Levels: a b c -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Hi, just use factor( ,levels=...) as in> a <- c("a", "g", "s", "s", "d", "s") > a <- factor(a) > a[1] a g s s d s Levels: a d g s> a <- factor(a, levels=c("s", "d", "a", "g")) > a[1] a g s s d s Levels: s d a g Cheers! Ott Toomet On 15 Feb 2002, Ross Darnell wrote:> > I would like to define the order of the levels of a factor. > > The relevel function would work but since I have 20 levels I would prefer > to declare the order explicitly. Using a smaller example > > levels(oldfactor) > "b1" "b2" "r1" "r2" > nufactor <- order(oldfactor,order=c("b1","r1","b2","r2")) # my fabricated function > levels(nufactor) > "b1" "r1" "b2" "r2" > > Thanks in advance for any help. > > Regards > Ross Darnell > >-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._