Bắt đầu ngayBắt đầu miễn phí

Mẹo với fct_relevel()

Trong bài trước, tên của từng level phải được viết đầy đủ để sắp xếp lại factor một cách tường minh. Nhưng đôi khi bạn có rất nhiều level và chỉ cần di chuyển một vài level. Thay vì phải liệt kê tất cả, bạn có thể dùng một số đối số tiện ích trong fct_relevel() để đỡ gõ. Hãy thử chúng trên biến FormalEducation trong multiple_choice_responses; hãy xem các level của multiple_choice_responses$FormalEducation trong console để bắt đầu.

Bài tập này là một phần của khóa học

Dữ liệu phân loại trong Tidyverse

Xem khóa học

Hướng dẫn bài tập

  • Trong ba lời gọi mutate, hãy thay đổi FormalEducation như sau:
    • Đưa "I did not complete any formal education past high school" và "Some college/university study without earning a bachelor's degree" lên đầu.
    • Đưa "I prefer not to answer" xuống thành level cuối cùng.
    • Đưa "Doctoral degree" thành level thứ sáu (sau level thứ năm).

Bài tập tương tác thực hành trực tiếp

Hãy thử làm bài tập này bằng cách hoàn thành đoạn mã mẫu này.

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()
Chỉnh sửa và Chạy Mã