Trucos de fct_relevel()
En el ejercicio anterior, hubo que escribir el nombre de cada nivel para reordenar el factor de forma explícita. Pero a veces hay muchos niveles y solo necesitas mover uno. En lugar de escribir todos los niveles, puedes usar algunos argumentos auxiliares en fct_relevel() para ahorrarte tecleo. Probemos algunos con la variable FormalEducation en multiple_choice_responses; consulta los niveles de multiple_choice_responses$FormalEducation en la consola para empezar.
Este ejercicio forma parte del curso
Datos categóricos en el Tidyverse
Instrucciones del ejercicio
- En tres llamadas a mutate, cambia
FormalEducationde las siguientes formas:- Mueve "I did not complete any formal education past high school" y "Some college/university study without earning a bachelor's degree" al frente.
- Mueve "I prefer not to answer" para que sea el último nivel.
- Mueve "Doctoral degree" para que sea el sexto nivel (después del quinto nivel).
ejercicio interactivo práctico
Prueba este ejercicio completando este código de ejemplo.
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()