By default, R will order character vectors in the order that the words appear or alphabetically order the levels of a factor variable.For example, suppose that you used the code below to create a data.frame of information about three species of iris.

> df <- read.csv("data/Iris.csv")
> str(df)
'data.frame':   150 obs. of  5 variables:
 $ seplen : int  50 46 46 51 55 48 52 49 44 50 ...
 $ sepwid : int  33 34 36 33 35 31 34 36 32 35 ...
 $ petlen : int  14 14 10 17 13 16 14 14 13 16 ...
 $ petwid : int  2 3 2 5 2 2 2 1 2 6 ...
 $ species: chr  "setosa" "setosa" "setosa" "setosa" ...

In this case suppose that we want to consider the species in a specific order. First, we need to see what names are present in the species variable.

> unique(df$species)
[1] "setosa"     "versicolor" "virginica" 

Second, we need to change the data type of species from character to a factor. This is done with factor(). However, at the same time we can set the order of the groups by setting the levels= argument to a vector of level names that are spelled exactly as they appear in the original variable, but are placed in the order you wish. For example, suppose that you would like versicolor to be listed fist, followed by setosa and then viriginica.

> df$species <- factor(df$species,levels=c("versicolor","setosa","virginica"))

You can then use levels() to see the ordering of the new factor variable.

> levels(df$species)
[1] "versicolor" "setosa"     "virginica" 

The new order is also seen in the following two frequency tables.

> xtabs(~species,data=df)
species
versicolor     setosa  virginica 
        50         50         50