Хитрощі `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()