The slice_min and slice_max verbs

Data Manipulation with dplyr

James Chapman

Curriculum Manager, DataCamp

slice_max()

  • Returns the largest observations in each group
counties_selected <- counties %>%
  select(state, county, population, unemployment, income)

counties_selected %>%
  group_by(state) %>%
  slice_max(population, n = 1)
Data Manipulation with dplyr

slice_max() output

# A tibble: 50 x 5
# Groups:   state [50]
   state       county                 population unemployment income
   <chr>       <chr>                       <dbl>        <dbl>  <dbl>
 1 Alabama     Jefferson                  659026          9.1  45610
 2 Alaska      Anchorage Municipality     299107          6.7  78326
 3 Arizona     Maricopa                  4018143          7.7  54229
 4 Arkansas    Pulaski                    390463          7.5  46140
 5 California  Los Angeles              10038388         10    56196
 6 Colorado    El Paso                    655024          8.4  58206
 7 Connecticut Fairfield                  939983          9    84233
 8 Delaware    New Castle                 549643          7.4  65476
 9 Florida     Miami-Dade                2639042         10    43129
10 Georgia     Fulton                     983903          9.9  57207
# … with 40 more rows
Data Manipulation with dplyr

slice_min()

  • Returns the smallest observations in each group
counties_selected %>%
  group_by(state) %>%
  slice_min(unemployment, n = 1)
Data Manipulation with dplyr

slice_min() output

# A tibble: 51 × 5
# Groups:   state [50]
   state       county                     population unemployment income
   <chr>       <chr>                           <dbl>        <dbl>  <dbl>
 1 Alabama     Shelby                         203530          5.5  70187
 2 Alaska      Aleutians West Census Area       5684          2.1  84306
 3 Arizona     Maricopa                      4018143          7.7  54229
 4 Arkansas    Benton                         238198          4.2  56239
 5 California  Marin                          258349          5.7  93257
 6 Colorado    Jackson                          1335          1.5  46014
 7 Connecticut Middlesex                      165165          6    79893
 8 Delaware    New Castle                     549643          7.4  65476
 9 Florida     Monroe                          75901          6    57290
10 Georgia     Bacon                           11222          4.4  37162
# … with 41 more rows
Data Manipulation with dplyr

Number of observations

counties_selected %>%
  group_by(state) %>%
  slice_max(unemployment, n = 3)
# A tibble: 153 × 5
# Groups:   state [50]
   state    county                    population unemployment income
   <chr>    <chr>                          <dbl>        <dbl>  <dbl>
 1 Alabama  Conecuh                        12865         22.6  24900
 2 Alabama  Wilcox                         11235         20.8  23750
 3 Alabama  Monroe                         22217         20.7  27257
 4 Alaska   Northwest Arctic Borough        7732         21.9  63648
 5 Alaska   Yukon-Koyukuk Census Area       5644         18.2  38491
 6 Alaska   Bethel Census Area             17776         17.6  51012
 7 Arizona  Navajo                        107656         19.8  35921
 8 Arizona  Apache                         72124         18.2  31757
 9 Arizona  Graham                         37407         14.1  45964
10 Arkansas Phillips                       20391         18.1  26844
# … with 143 more rows
Data Manipulation with dplyr

Let's practice!

Data Manipulation with dplyr

Preparing Video For Download...