10.19 Subset of Rows Within Groups

20200814 The dplyr::slice() command understands groups and so the indicies supplied will apply to each group. To select, for example, the second row of each group, simply supply 2 as the argument. Notice the rows selected are all on the second of the month.

ds %>%
  group_by(location) %>%
  slice(2)
## # A tibble: 49 × 24
## # Groups:   location [49]
##    date       location   min_t…¹ max_t…² rainf…³ evapo…⁴ sunsh…⁵ wind_…⁶ wind_…⁷
##    <date>     <chr>        <dbl>   <dbl>   <dbl>   <dbl>   <dbl> <ord>     <dbl>
##  1 2008-07-02 Adelaide      12.7    15.8     0.8     1.4     7.8 SW           35
##  2 2008-12-02 Albany        14.7    21       0       5.4     9.1 W            39
##  3 2008-12-02 Albury         7.4    25.1     0       4.8     8.5 WNW          44
##  4 2008-12-02 AliceSpri…    21      39.1     1.2     9      12.2 NNW          41
##  5 2009-01-02 BadgerysC…    14.7    26.1     0       4.8     8.5 SE           46
##  6 2008-12-02 Ballarat       4.3    19.5     0.4     4.8     8.5 WNW          44
##  7 2008-12-02 Bendigo        6.2    23       0       8.8     8.5 W            48
##  8 2008-07-02 Brisbane       8.1    22.9     0       2       9.8 W            30
##  9 2008-12-02 Cairns        24.2    32.3     8.4     6.6     5.3 NE           35
## 10 2007-11-02 Canberra      14      26.9     3.6     4.4     9.7 ENE          39
## # … with 39 more rows, 15 more variables: wind_dir_9am <ord>,
## #   wind_dir_3pm <ord>, wind_speed_9am <dbl>, wind_speed_3pm <dbl>,
## #   humidity_9am <dbl>, humidity_3pm <dbl>, pressure_9am <dbl>,
## #   pressure_3pm <dbl>, cloud_9am <dbl>, cloud_3pm <dbl>, temp_9am <dbl>,
## #   temp_3pm <dbl>, rain_today <fct>, risk_mm <dbl>, rain_tomorrow <fct>, and
## #   abbreviated variable names ¹​min_temp, ²​max_temp, ³​rainfall, ⁴​evaporation,
## #   ⁵​sunshine, ⁶​wind_gust_dir, ⁷​wind_gust_speed


Your donation will support ongoing availability and give you access to the PDF version of this book. Desktop Survival Guides include Data Science, GNU/Linux, and MLHub. Books available on Amazon include Data Mining with Rattle and Essentials of Data Science. Popular open source software includes rattle, wajig, and mlhub. Hosted by Togaware, a pioneer of free and open source software since 1984. Copyright © 1995-2022 Graham.Williams@togaware.com Creative Commons Attribution-ShareAlike 4.0