開始使用免費開始

從四個動詞中做選擇

在本章中你學到了四個動詞:select()mutate()relocate(),以及 rename()。在這裡,你要為每個情境選出合適的動詞。你不需要更動小括號內的任何內容。

本練習屬於課程

使用 dplyr 進行資料操作

檢視課程

練習說明

  • 選擇正確的動詞,將 unemployment 欄位名稱改為 unemployment_rate
  • 選擇正確的動詞,只保留 statecounty 以及欄名中包含 poverty 的欄位。
  • 計算一個名為 fraction_women 的新欄位,內容為女性占人口的比例,且不要刪除任何欄位。
  • 只保留三個欄位:statecounty,以及 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)
編輯並執行程式碼