從四個動詞中做選擇
在本章中你學到了四個動詞:select()、mutate()、relocate(),以及 rename()。在這裡,你要為每個情境選出合適的動詞。你不需要更動小括號內的任何內容。
本練習屬於課程
使用 dplyr 進行資料操作
練習說明
- 選擇正確的動詞,將
unemployment欄位名稱改為unemployment_rate。 - 選擇正確的動詞,只保留
state、county以及欄名中包含poverty的欄位。 - 計算一個名為
fraction_women的新欄位,內容為女性占人口的比例,且不要刪除任何欄位。 - 只保留三個欄位:
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)