시작하기무료로 시작하기

fct_relevel() 요령 익히기

이전 연습 문제에서는 요인을 재정렬하려면 각 레벨 이름을 모두 써야 했어요. 하지만 레벨이 많고 일부만 옮기면 될 때도 있죠. 이런 경우에는 fct_relevel()의 보조 인자를 사용해 타이핑을 줄일 수 있어요. multiple_choice_responsesFormalEducation 변수에 몇 가지를 적용해 보죠. 먼저 콘솔에서 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()
코드 편집 및 실행