เทคนิคของ fct_relevel()
ในแบบฝึกหัดก่อนหน้า ต้องระบุชื่อของแต่ละระดับออกมาทั้งหมดเพื่อเรียงลำดับใหม่ แต่บางครั้งมีระดับจำนวนมากและต้องการย้ายเพียงระดับเดียว แทนที่จะเขียนชื่อทุกระดับ สามารถใช้อาร์กิวเมนต์ช่วยใน fct_relevel() เพื่อประหยัดเวลาได้ มาลองใช้กับตัวแปร FormalEducation ในชุดข้อมูล multiple_choice_responses กัน โดยให้ดูระดับของ multiple_choice_responses$FormalEducation ใน console ก่อนเริ่มต้น
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
ข้อมูลเชิงหมวดหมู่ใน 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" ไปไว้เป็นระดับที่ 6 (ถัดจากระดับที่ 5)
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
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()