To create factors in R, you make use of the function factor(). First thing that you have to do is create a vector that contains all the observations that belong to a limited number of categories. For example, sex_vector contains the sex of 5 different individuals:
sex_vector <- c("Male","Female","Female","Male","Male")
It is clear that there are two categories, or in R-terms 'factor levels', at work here: "Male" and "Female".
The function factor() will encode the vector as a factor:
factor_sex_vector <- factor(sex_vector)
sex_vector to a factor with factor() and assign the result to factor_sex_vectorfactor_sex_vector and assert that R prints out the factor levels below the actual values.
# Sex vector
sex_vector <- c("Male", "Female", "Female", "Male", "Male")
# Convert sex_vector to a factor
factor_sex_vector <-
# Print out factor_sex_vector
# Sex vector
sex_vector <- c("Male", "Female", "Female", "Male", "Male")
# Convert sex_vector to a factor
factor_sex_vector <- factor(sex_vector)
# Print out factor_sex_vector
factor_sex_vector
test_object("factor_sex_vector",
incorrect_msg = "Did you assign the factor of `sex_vector` to `factor_sex_vector`?")
test_output_contains("factor_sex_vector", incorrect_msg = "Don't forget to print out `factor_sex_vector`!")
success_msg("Great! If you want to find out more about the `factor()` function, do not hesitate to type `?factor` in the console. This will open up a help page. Continue to the next exercise.");
Simply use the function factor() on sex_vector. Have a look at the assignment, the answer is already there somewhere...