检查水平数量
在探索数据集时,dplyr 还有两个很实用的函数。第一个是 slice_max(var, n = x),它会根据 var 的取值,选出数据集中排在前面的 x 行。另一个是 pull(),用于提取某一列并去掉列名,只保留该列的值。
例如,若要从经典数据集 mtcars 中获取作为数值集合的前 2 个 mpg 值,可以这样写:
mtcars %>%
slice_max(mpg, n = 2) %>%
pull(mpg)
这会得到:
[1] 32.4 33.9
本练习是课程的一部分
Tidyverse 中的分类数据
练习说明
- 使用
slice_max()输出具有最多因子水平的 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
___