For some reason that’s probably a short-coming of my brain, it always takes me longer than it should to manipulate the order of factor levels. My head just has trouble with `relevel`, `reorder`, etc… So I’ve written a function that works with my brain better:

``````reorder2 <- function(x, X){
X <- rev(X)
for(i in seq_along(X)) x <- relevel(x, X[i])
x
}``````

`x` is a factor and `X` is a character vector of levels in the order in which you want them to be in `x`. So for example,

``````
> levels(fac)
[1] "arctic"      "equatorial"  "midlatitude" "subarctic"   "subtropical"
[6] "tropical"
``````

That’s not right! I want this more reasonable order,

``````
> cor.ord <- c('equatorial','tropical','subtropical','midlatitude','subarctic','arctic')
``````

So I pass it to `reorder2`,

``````
> fac <- reorder2(fac, cor.ord)
``````

And now everything’s fine,

``````
> levels(fac)
[1] "equatorial"  "tropical"    "subtropical" "midlatitude" "subarctic"
[6] "arctic"
``````
```fac <- factor(fac, levels=cor.ord)