Comparing ordered factors
Having a bad day at work, 'data analyst number two' enters your office and starts complaining that 'data analyst number five' is slowing down the entire project. Since you know that 'data analyst number two' has the reputation of being a smarty-pants, you first decide to check if his statement is true.
The fact that factor_speed_vector
is now ordered enables us to compare different elements (the data analysts in this case). You can simply do this by using the well-known operators.
This is a part of the course
“Introduction to R”
Exercise instructions
- Use
[2]
to select fromfactor_speed_vector
the factor value for the second data analyst. Store it asda2
. - Use
[5]
to select thefactor_speed_vector
factor value for the fifth data analyst. Store it asda5
. - Check if
da2
is greater thanda5
; simply print out the result. Remember that you can use the>
operator to check whether one element is larger than the other.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Create factor_speed_vector
speed_vector <- c("medium", "slow", "slow", "medium", "fast")
factor_speed_vector <- factor(speed_vector, ordered = TRUE, levels = c("slow", "medium", "fast"))
# Factor value for second data analyst
da2 <-
# Factor value for fifth data analyst
da5 <-
# Is data analyst 2 faster than data analyst 5?