重编码主题
还有一步数据清洗能让结果更易理解。当前的主题用两个字母的代码表示:
- 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"))