Good job! You might remember the Levels: line while outputting some factors. What if you are interested in all the possible values of your factor values?

To output all the levels of a vector containing categorical values, use the levels() function, passing the vector as the parameter.

Well, these levels can be shuffled, and nothing will change. However, we may face factor variables that can be ordered. For example, we can call a person tall, medium, or short in terms of height. In that case, tall > medium > short. In R, you can consider this moment. You need to specify the ordered parameter to T. This will order variables alphabetically (if values are textual) or according to math laws (if numerical). The math laws are most likely acceptable for us, but alphabetical order is surely not. To set specific order, you need to also set the labels parameter to the vector of values in the ascending order. Watch out for the example.

You definitely can notice the difference. Practice by yourself!


Given vector of grades on a scale 'A-F'. You need to transform them into factor, considering the order: 'F < D < C < B < A'

  1. Transform vector grades into factor type and save within grades_f variable considering the necessary order ('F < D < C < B < A').
  2. Output entire grades_f variable.

Everything was clear?

Section 3. Chapter 3
toggle bottom row