One more note. There is actually a valid reason to use `levels<-`, and that is to change the spelling of the levels while maintaining the existing order.
> tmp <- factor(c("mm", "cm", "dm", "m", "km")) > tmp [1] mm cm dm m km Levels: cm dm km m mm > levels(tmp) <- c("centimeter","decimeter","kilometer","meter","millimeter") > tmp [1] millimeter centimeter decimeter meter kilometer 5 Levels: centimeter decimeter kilometer ... millimeter > levels(tmp) [1] "centimeter" "decimeter" "kilometer" "meter" [5] "millimeter" > Rich On Fri, Jun 5, 2015 at 4:47 AM, Gerrit Eichner <gerrit.eich...@math.uni-giessen.de> wrote: > Hello, everyone, > > aside from Rolf's hint (and Richard's warning!) you could also consider > relevel(): > > viagraData$dose <- relevel( viagraData$dose, ref = "placebo") > > Hth -- Gerrit > > > On Fri, 5 Jun 2015, Rolf Turner wrote: > >> On 05/06/15 11:08, Jim Lemon wrote: >>> >>> Hi James, >>> You can change the order of levels like this: >>> >>> levels(viagraData$dose)<-c("placebo","low dose","high dose") >> >> >> <SNIP> >> >> As Richard Heiberger has pointed out, this is wrong. >> >> What *does* work is: >> >> viagraData$dose)<-factor(viagraData$dose, >> levels=c("placebo","low dose","high dose") >> >> This is a trap into which many a Young Player (including my very good >> self) has fallen. >> >> cheers, >> >> Rolf Turner >> >> >> >> -- >> Technical Editor ANZJS >> Department of Statistics >> University of Auckland >> Phone: +64-9-373-7599 ext. 88276 >> >> ______________________________________________ >> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >> 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@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.