檢視層級數量
dplyr 還有兩個在探索資料集時很好用的函式。第一個是 slice_max(var, n = x),可依 var 的值取出資料集前 x 列。另一個是 pull(),可以擷取某個欄位並移除欄位名稱,只保留該欄位的值。
例如,如果你想從經典的 mtcars 資料集中,取得作為一組值的前兩個 mpg 最高值,可以這樣寫:
mtcars %>%
slice_max(mpg, n = 2) %>%
pull(mpg)
這會得到:
[1] 32.4 33.9
本練習屬於課程
Tidyverse 的類別資料
練習說明
- 使用
slice_max()印出擁有最多 factor 層級數的前 3 列。 - 針對變數
CurrentJobTitleSelect,使用pull取出它的層級數。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Select the 3 rows with the highest number of levels
number_of_levels %>%
___(num_levels, n = 3)
number_of_levels %>%
# Filter for where the column called variable equals CurrentJobTitleSelect
filter(___) %>%
# Pull num_levels
___