重新編碼主題
還有最後一步資料清理,能讓結果更容易解讀。現在的主題以兩個字母的代碼表示:
- me:Palestinian conflict(巴勒斯坦衝突)
- nu:Nuclear weapons and nuclear material(核子武器與核子材料)
- di:Arms control and disarmament(軍備管制與裁軍)
- hr:Human rights(人權)
- co:Colonialism(殖民主義)
- ec:Economic development(經濟發展)
為了讓你更容易解讀資料,請將這些代碼重新編碼為完整名稱。你可以使用 dplyr 的 recode() 函式,將指定的值替換為你提供的新值:
example <- c("apple", "banana", "apple", "orange")
recode(example,
apple = "plum",
banana = "grape")
本練習屬於課程
案例研究:R 中的探索性資料分析
練習說明
在 mutate() 中使用 dplyr 的 recode() 函式,將 votes_gathered 資料框中的每個兩字母代碼替換為對應的完整名稱。將結果儲存為 votes_tidied。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Replace the two-letter codes in topic: votes_tidied
votes_tidied <- ___ %>%
mutate(topic = recode(___,
___ = "Palestinian conflict",
___ = "Nuclear weapons and nuclear material",
___ = "Arms control and disarmament",
___ = "Human rights",
___ = "Colonialism",
___ = "Economic development"))