Tricks of fct_relevel()
In the previous exercise, the name of each level had to be written out to explicitly reorder the factor. But sometimes you have many levels and only need to move one. Instead of writing out all the levels, you can use some helper arguments in fct_relevel() to save you some typing. Let's try some of them out on the variable FormalEducation in multiple_choice_responses; view the levels of the multiple_choice_responses$FormalEducation in the console to get started.
Bu egzersiz
Categorical Data in the Tidyverse
kursunun bir parçasıdırEgzersiz talimatları
- In three mutate calls, change
FormalEducationin the following ways:- Move "I did not complete any formal education past high school" and "Some college/university study without earning a bachelor's degree" to the front.
- Move "I prefer not to answer" to be the last level.
- Move "Doctoral degree" to be the sixth level (after the fifth level).
Uygulamalı interaktif egzersiz
Bu örnek kodu tamamlayarak bu egzersizi bitirin.
multiple_choice_responses %>%
# Move "I did not complete any formal education past high school" and "Some college/university study without earning a bachelor's degree" to the front
mutate(FormalEducation = fct_relevel(FormalEducation, ___, "Some college/university study without earning a bachelor's degree")) %>%
# Move "I prefer not to answer" to be the last level.
mutate(FormalEducation = fct_relevel(FormalEducation, ___, after = Inf)) %>%
# Move "Doctoral degree" to be after the 5th level
mutate(FormalEducation = fct_relevel(FormalEducation, ___, ____)) %>%
# Examine the new level order
pull(FormalEducation) %>%
levels()