10.17 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 x 24
## # Groups:   location [49]
##    date       location      min_temp max_temp rainfall evaporation sunshine
##    <date>     <chr>            <dbl>    <dbl>    <dbl>       <dbl>    <dbl>
##  1 2008-07-02 Adelaide          12.7     15.8      0.8         1.4      7.8
##  2 2008-12-02 Albany            14.7     21        0           5.4      9.1
##  3 2008-12-02 Albury             7.4     25.1      0           4.8      8.5
##  4 2008-12-02 AliceSprings      21       39.1      1.2         9       12.2
##  5 2009-01-02 BadgerysCreek     14.7     26.1      0           4.8      8.5
##  6 2008-12-02 Ballarat           4.3     19.5      0.4         4.8      8.5
##  7 2008-12-02 Bendigo            6.2     23        0           8.8      8.5
##  8 2008-07-02 Brisbane           8.1     22.9      0           2        9.8
##  9 2008-12-02 Cairns            24.2     32.3      8.4         6.6      5.3
## 10 2007-11-02 Canberra          14       26.9      3.6         4.4      9.7
## # … with 39 more rows, and 17 more variables: wind_gust_dir <ord>,
## #   wind_gust_speed <dbl>, 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>


Your donation will support ongoing development 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-2021 Graham.Williams@togaware.com Creative Commons Attribution-ShareAlike 4.0.