What's a factor and why would you use it? (3)

There are two types of categorical variables: a nominal categorical variable and an ordinal categorical variable.

A nominal variable is a categorical variable without an implied order. This means that it is impossible to say that 'one is worth more than the other'. For example, think of the categorical variable animals_vector with the categories "Elephant", "Giraffe", "Donkey" and "Horse". Here, it is impossible to say that one stands above or below the other. (Note that some of you might disagree ;-) ).

In contrast, ordinal variables do have a natural ordering. Consider for example the categorical variable temperature_vector with the categories: "Low", "Medium" and "High". Here it is obvious that "Medium" stands above "Low", and "High" stands above "Medium".

Instruction

# Animals animals_vector <- c("Elephant", "Giraffe", "Donkey", "Horse") factor_animals_vector <- factor(animals_vector) factor_animals_vector # Temperature temperature_vector <- c("High", "Low", "High","Low", "Medium") factor_temperature_vector <- factor(temperature_vector, order = TRUE, levels = c("Low", "Medium", "High")) factor_temperature_vector # Animals animals_vector <- c("Elephant", "Giraffe", "Donkey", "Horse") factor_animals_vector <- factor(animals_vector) factor_animals_vector # Temperature temperature_vector <- c("High", "Low", "High","Low", "Medium") factor_temperature_vector <- factor(temperature_vector, order = TRUE, levels = c("Low", "Medium", "High")) factor_temperature_vector msg <- "Do not change anything about the sample code. Simply hit the Submit Answer button and inspect the solution!" test_object("animals_vector", undefined_msg = msg, incorrect_msg = msg) test_object("temperature_vector", undefined_msg = msg, incorrect_msg = msg) test_object("factor_animals_vector", undefined_msg = msg, incorrect_msg = msg) test_output_contains("factor_animals_vector", incorrect_msg = msg) test_object("factor_temperature_vector", undefined_msg = msg, incorrect_msg = msg) test_output_contains("factor_temperature_vector", incorrect_msg = msg) success_msg("Can you already tell what's happening in this exercise? Awesome! Continue to the next exercise and get into the details of factor levels.")

Just click the 'Submit Answer' button and look at the console. Notice how R indicates the ordering of the factor levels for ordinal categorical variables.

Previous: 4-2 | What's a factor and why would you use it? (2)

Next: 4-4 | Factor levels

Back to Main