在四个动词中做选择
本章中,您学习了四个动词:select()、mutate()、relocate() 和 rename()。在这里,您将为每种情形选择合适的动词。您不需要修改圆括号内的任何内容。
本练习是课程的一部分
使用 dplyr 进行数据处理
练习说明
- 选择合适的动词,将
unemployment列名改为unemployment_rate。 - 选择合适的动词,仅保留
state、county以及所有包含poverty的列。 - 计算一个名为
fraction_women的新列,表示女性占总人口的比例,且不删除任何列。 - 仅保留 3 列:
state、county,以及employed / population,并将其命名为employment_rate。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Change the name of the unemployment column
counties %>%
___(unemployment_rate = unemployment)
# Keep the state and county columns, and the columns containing poverty
counties %>%
___(state, county, contains("poverty"))
# Calculate the fraction_women column without dropping the other columns
counties %>%
___(fraction_women = women / population)
# Move the region column to before state
counties %>%
___(region, .before = state)