Skip to content

Instantly share code, notes, and snippets.

@brshallo
Last active March 25, 2021 01:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brshallo/fbd0a98f3ed1a8ee1ec66f4476715c3f to your computer and use it in GitHub Desktop.
Save brshallo/fbd0a98f3ed1a8ee1ec66f4476715c3f to your computer and use it in GitHub Desktop.
library(tidyverse)

sample_n_of <- function(data, size, ...) {
  dots <- quos(...)
  
  group_ids <- data %>% 
    group_by(!!! dots) %>% 
    group_indices()
  
  sampled_groups <- sample(unique(group_ids), size)
  
  data %>% 
    filter(group_ids %in% sampled_groups)
}

set.seed(1234)
mpg %>% 
  group_by(model) %>% 
  slice_sample(n = 2) %>%
  ungroup()
#> # A tibble: 76 x 11
#>    manufacturer model    displ  year   cyl trans   drv     cty   hwy fl    class
#>    <chr>        <chr>    <dbl> <int> <int> <chr>   <chr> <int> <int> <chr> <chr>
#>  1 toyota       4runner~   2.7  1999     4 manual~ 4        15    20 r     suv  
#>  2 toyota       4runner~   3.4  1999     6 manual~ 4        15    17 r     suv  
#>  3 audi         a4         2.8  1999     6 auto(l~ f        16    26 p     comp~
#>  4 audi         a4         2    2008     4 auto(a~ f        21    30 p     comp~
#>  5 audi         a4 quat~   3.1  2008     6 auto(s~ 4        17    25 p     comp~
#>  6 audi         a4 quat~   2.8  1999     6 auto(l~ 4        15    25 p     comp~
#>  7 audi         a6 quat~   2.8  1999     6 auto(l~ 4        15    24 p     mids~
#>  8 audi         a6 quat~   4.2  2008     8 auto(s~ 4        16    23 p     mids~
#>  9 nissan       altima     2.5  2008     4 manual~ f        23    32 r     mids~
#> 10 nissan       altima     2.5  2008     4 auto(a~ f        23    31 r     mids~
#> # ... with 66 more rows

mpg %>% 
  sample_n_of(size = 2, model)
#> # A tibble: 12 x 11
#>    manufacturer model   displ  year   cyl trans   drv     cty   hwy fl    class 
#>    <chr>        <chr>   <dbl> <int> <int> <chr>   <chr> <int> <int> <chr> <chr> 
#>  1 audi         a6 qua~   2.8  1999     6 auto(l~ 4        15    24 p     midsi~
#>  2 audi         a6 qua~   3.1  2008     6 auto(s~ 4        17    25 p     midsi~
#>  3 audi         a6 qua~   4.2  2008     8 auto(s~ 4        16    23 p     midsi~
#>  4 ford         mustang   3.8  1999     6 manual~ r        18    26 r     subco~
#>  5 ford         mustang   3.8  1999     6 auto(l~ r        18    25 r     subco~
#>  6 ford         mustang   4    2008     6 manual~ r        17    26 r     subco~
#>  7 ford         mustang   4    2008     6 auto(l~ r        16    24 r     subco~
#>  8 ford         mustang   4.6  1999     8 auto(l~ r        15    21 r     subco~
#>  9 ford         mustang   4.6  1999     8 manual~ r        15    22 r     subco~
#> 10 ford         mustang   4.6  2008     8 manual~ r        15    23 r     subco~
#> 11 ford         mustang   4.6  2008     8 auto(l~ r        15    22 r     subco~
#> 12 ford         mustang   5.4  2008     8 manual~ r        14    20 p     subco~

Created on 2021-03-24 by the reprex package (v0.3.0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment