concept levels() in category R

This is an excerpt from Manning's book Exploring Data with R.
Using unique values for the vector is pretty instructive here since the 16 unique values have 16 factor levels. But what’s a level in this world of factors? It’s an integer value that determines the ordering of the strings. There are a number of base R functions that let us do diagnostics on a factor. To see all the levels and, separately, the integer values associated with them, we can use the levels() and as.integer() functions (Listings 9.21 and 9.22).
Listing 9.21. Getting all the levels for the state_fct factor using the levels() function
```{r state_fct_levels} levels(state_fct) ``` #> [1] "Baden-Württemberg" "Bayern" "Berlin" #> [4] "Brandenburg" "Bremen" "Hamburg" #> [7] "Hesse" "Mecklenburg-Vorpommern" "Niedersachsen" #> [10] "Nordrhein-Westfalen" "Rheinland-Pfalz" "Saarland" #> [13] "Sachsen" "Sachsen-Anhalt" "Schleswig-Holstein" #> [16] "Thüringen"Listing 9.22. Getting the integer values for the factor levels in state_fct with as.integer()
```{r state_fct_as_integer} as.integer(state_fct) ``` #> [1] 10 2 3 9 5 13 7 16 1 14 6 15 11 4 8 12When getting the levels through levels() we see that the state names are in alphabetical order. This is the default behavior of R when transforming character data to factors (i.e., during data import) and it’s likely what has occurred during the creation of the german_cities dataset. So, in the output of Listing 9.22, 10 is Nordrhein-Westfalen, 2 is Bayern, and so on. It’s the same order of cities as in the state_fct vector but the factors were based on the alphabetical ordering of the cities.