1. The slice_min and slice_max verbs
What if instead of aggregating each state, we wanted to find only the largest or smallest counties in each state? dplyr's slice_min and slice_max verbs allow us to extract the most extreme observations from each group.
2. slice_max()
Like summarize(), slice_max() operates on a grouped table, and returns the largest observations in each group. The function takes two arguments: the column we want to base the ordering on, and the number of observations to extract from each group, specified with the n argument. In this example, grouping by state and then calling slice_max on the population column with n equals 1 returns the county with the highest population in each state.
3. slice_max() output
This tells us, for example, that Jefferson is the county with the highest population in Alabama with a population of 659,000. Notice that it kept the other columns in this table, in this case, unemployment and income.
4. slice_min()
Similarly, slice_min() returns the smallest observations in each group.
Instead of looking at the population, we could find the county with the lowest unemployment rate from each state by calling slice_min() on the unemployment column.
5. slice_min() output
The output shows that, for example, Jackson is the county with the lowest unemployment rate in the state of Colorado, with an unemployment rate of 1-point-5 percent.
6. Number of observations
We can use other values of n in our slice_min() and slice_max() calls, and get that many counties from each state.
For instance, slice_max() on unemployment with n = 3 will return the three counties in each state with the highest unemployment rate. For Alabama, these turn out to be named Conecuh, Monroe, and Wilcox.
The slicing verbs are often used when creating visualizations, where we may want to highlight the extreme observations on the plot.
7. Let's practice!
In the exercises, you'll try grabbing other extreme observations from groups. You'll also combine all the aggregations you've learned so far in this chapter to answer increasingly complex questions.