Handigheidjes van fct_relevel()
In de vorige oefening moest je de naam van elk niveau uitschrijven om de factor expliciet te herordenen. Maar soms heb je veel niveaus en hoef je er maar één te verplaatsen. In plaats van alle niveaus te noteren, kun je in fct_relevel() enkele hulpargumenten gebruiken om typwerk te besparen. Laten we er een paar uitproberen op de variabele FormalEducation in multiple_choice_responses; bekijk de niveaus van multiple_choice_responses$FormalEducation in de console om te beginnen.
Deze oefening maakt deel uit van de cursus
Categorische gegevens in de Tidyverse
Oefeninstructies
- Pas
FormalEducationin drie mutate-calls als volgt aan:- Verplaats "I did not complete any formal education past high school" en "Some college/university study without earning a bachelor's degree" naar voren.
- Zet "I prefer not to answer" als laatste niveau.
- Zet "Doctoral degree" als zesde niveau (na het vijfde niveau).
Praktische interactieve oefening
Probeer deze oefening eens door deze voorbeeldcode in te vullen.
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()