Возможности fct_relevel()
В предыдущем упражнении для явного изменения порядка факторов приходилось прописывать название каждого уровня. Однако когда уровней много, а переместить нужно лишь один, можно воспользоваться вспомогательными аргументами функции fct_relevel() — это существенно сэкономит время. Попробуем применить их к переменной FormalEducation из multiple_choice_responses. Чтобы начать, посмотрите в консоли на уровни multiple_choice_responses$FormalEducation.
Это упражнение является частью курса
Категориальные данные в Tidyverse
Инструкции к упражнению
- В трёх вызовах
mutateизменитеFormalEducationследующим образом:- Переместите «I did not complete any formal education past high school» и «Some college/university study without earning a bachelor's degree» в начало.
- Переместите «I prefer not to answer» на последнее место.
- Переместите «Doctoral degree» на шестую позицию (после пятого уровня).
Интерактивное практическое упражнение
Попробуйте выполнить это упражнение, дополнив этот пример кода.
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()