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?
This exercise is part of the course
Introduction to R
Master the basics of data analysis in R, including vectors, lists, and data frames, and practice R with real data sets.
Data often falls into a limited number of categories. For example, human hair color can be categorized as black, brown, blond, red, grey, or white—and perhaps a few more options for people who color their hair. In R, categorical data is stored in factors. Factors are very important in data analysis, so start learning how to create, subset, and compare them now.
Exercise 1: What's a factor and why would you use it?Exercise 2: What's a factor and why would you use it? (2)Exercise 3: What's a factor and why would you use it? (3)Exercise 4: Factor levelsExercise 5: Summarizing a factorExercise 6: Battle of the sexesExercise 7: Ordered factorsExercise 8: Ordered factors (2)Exercise 9: Comparing ordered factorsWhat is DataCamp?
Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.