9.3 Random Sample

20200317

A common task is to randomly sample rows from a dataset. The dplyr::sample_frac() function will randomly choose a specified fraction (e.g. 20%) of the rows of the dataset:

ds %>% sample_frac(0.2)
## # A tibble: 55,082 × 24
##    date       location    min_temp max_temp rainfall evaporation sunshine
##    <date>     <chr>          <dbl>    <dbl>    <dbl>       <dbl>    <dbl>
##  1 2017-12-07 Darwin          28.2     34.5      0           8        9.9
##  2 2015-11-23 Bendigo         11       25.4      0          NA       NA  
##  3 2021-10-16 Wollongong      13.2     21.9     NA          NA       NA  
##  4 2024-01-13 Wollongong      21       26.4      0          NA       NA  
##  5 2020-04-15 Brisbane        16.7     27.4      0          NA       10  
##  6 2020-11-26 Sale            14.1     23.7      0          NA       NA  
##  7 2009-01-30 Nuriootpa       22       41.9      0          14.8     12.7
##  8 2013-06-11 Williamtown      9.9     20.5      0.6         1.4      9.5
##  9 2018-12-01 Sale             9.6     26.8      0          NA       NA  
## 10 2012-03-10 Townsville      24.2     30.7      0           5.8      3.5
## # ℹ 55,072 more rows
## # ℹ 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 <int>, humidity_3pm <int>,
## #   pressure_9am <dbl>, pressure_3pm <dbl>, cloud_9am <int>, cloud_3pm <int>,
## #   temp_9am <dbl>, temp_3pm <dbl>, rain_today <fct>, risk_mm <dbl>,
## #   rain_tomorrow <fct>

The next time you randomly sample the dataset the resulting sample will be different:

ds %>% sample_frac(0.2)
## # A tibble: 55,082 × 24
##    date       location    min_temp max_temp rainfall evaporation sunshine
##    <date>     <chr>          <dbl>    <dbl>    <dbl>       <dbl>    <dbl>
##  1 2013-11-07 Tuggeranong      4.7     31.1      0          NA         NA
##  2 2015-04-15 Ballarat        13.3     24.3      1.2        NA         NA
##  3 2020-11-01 Darwin          25.5     36.3      0           8         12
##  4 2013-05-25 Richmond         6.2     20.7      1.6        NA         NA
##  5 2011-11-22 Albury          11.9     22.7      2.6        NA         NA
##  6 2023-07-08 Woomera          7.1     18.5      0          NA         NA
##  7 2020-10-04 SalmonGums       4.5     15.9      0.6        NA         NA
##  8 2025-01-19 Ballarat        12       33        0          NA         NA
##  9 2018-06-28 Richmond         8.1     16.4      2.8         0.4       NA
## 10 2010-12-07 Adelaide        21.1     32.5      3.4         5.8        6
## # ℹ 55,072 more rows
## # ℹ 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 <int>, humidity_3pm <int>,
## #   pressure_9am <dbl>, pressure_3pm <dbl>, cloud_9am <int>, cloud_3pm <int>,
## #   temp_9am <dbl>, temp_3pm <dbl>, rain_today <fct>, risk_mm <dbl>,
## #   rain_tomorrow <fct>

To ensure the sample random sample each time use base::set.seed():

set.seed(72346)
ds %>% sample_frac(0.2)
## # A tibble: 55,082 × 24
##    date       location      min_temp max_temp rainfall evaporation sunshine
##    <date>     <chr>            <dbl>    <dbl>    <dbl>       <dbl>    <dbl>
##  1 2026-01-11 MountGinini        7.4     17.1      0          NA       NA  
##  2 2013-04-08 Nuriootpa         12.6     25.3      0           8       10.2
##  3 2017-11-03 Dartmoor           3.5     15        0.8        NA       NA  
##  4 2018-01-11 BadgerysCreek     17.3     27        0.2        NA       NA  
##  5 2011-10-27 Launceston         5.1     19.1      0          NA       NA  
##  6 2024-01-27 Nuriootpa          5.9     23.8      0           8       NA  
##  7 2021-04-11 Wollongong        15.1     19        0          NA       NA  
##  8 2022-01-15 Penrith           19.9     32.5      1.4        NA       NA  
##  9 2022-03-06 Ballarat          11       19.1      2          NA       NA  
## 10 2010-06-22 Dartmoor           5       14.8      0           0.6      4.8
## # ℹ 55,072 more rows
## # ℹ 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 <int>, humidity_3pm <int>,
## #   pressure_9am <dbl>, pressure_3pm <dbl>, cloud_9am <int>, cloud_3pm <int>,
## #   temp_9am <dbl>, temp_3pm <dbl>, rain_today <fct>, risk_mm <dbl>,
## #   rain_tomorrow <fct>
set.seed(72346)
ds %>% sample_frac(0.2)
## # A tibble: 55,082 × 24
##    date       location      min_temp max_temp rainfall evaporation sunshine
##    <date>     <chr>            <dbl>    <dbl>    <dbl>       <dbl>    <dbl>
##  1 2026-01-11 MountGinini        7.4     17.1      0          NA       NA  
##  2 2013-04-08 Nuriootpa         12.6     25.3      0           8       10.2
##  3 2017-11-03 Dartmoor           3.5     15        0.8        NA       NA  
##  4 2018-01-11 BadgerysCreek     17.3     27        0.2        NA       NA  
##  5 2011-10-27 Launceston         5.1     19.1      0          NA       NA  
##  6 2024-01-27 Nuriootpa          5.9     23.8      0           8       NA  
##  7 2021-04-11 Wollongong        15.1     19        0          NA       NA  
##  8 2022-01-15 Penrith           19.9     32.5      1.4        NA       NA  
##  9 2022-03-06 Ballarat          11       19.1      2          NA       NA  
## 10 2010-06-22 Dartmoor           5       14.8      0           0.6      4.8
## # ℹ 55,072 more rows
## # ℹ 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 <int>, humidity_3pm <int>,
## #   pressure_9am <dbl>, pressure_3pm <dbl>, cloud_9am <int>, cloud_3pm <int>,
## #   temp_9am <dbl>, temp_3pm <dbl>, rain_today <fct>, risk_mm <dbl>,
## #   rain_tomorrow <fct>


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