Truques do fct_relevel()
No exercício anterior, foi preciso escrever o nome de cada nível para reorganizar explicitamente o fator. Mas às vezes você tem muitos níveis e só precisa mover um. Em vez de escrever todos os níveis, você pode usar alguns argumentos auxiliares em fct_relevel() para economizar digitação. Vamos testar alguns deles na variável FormalEducation em multiple_choice_responses; visualize os níveis de multiple_choice_responses$FormalEducation no console para começar.
Este exercício faz parte do curso
Dados Categóricos no Tidyverse
Instruções do exercício
- Em três chamadas de mutate, altere
FormalEducationdas seguintes formas:- Mova "I did not complete any formal education past high school" e "Some college/university study without earning a bachelor's degree" para o início.
- Mova "I prefer not to answer" para ser o último nível.
- Mova "Doctoral degree" para ser o sexto nível (após o quinto nível).
Exercício interativo prático
Experimente este exercício completando este código de exemplo.
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()