Can I choose different sizes for each group using dplyr?

Okay, so I know that I can do something like this,

mtcars %>% 
group_by(cyl) %>% 
sample_n(2)

which will give me

Source: local data frame [6 x 11]
Groups: cyl [3]

 mpg   cyl  disp    hp  drat    wt  qsec    vs    am
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1  21.4     4 121.0   109  4.11 2.780 18.60     1     1
2  33.9     4  71.1    65  4.22 1.835 19.90     1     1
3  18.1     6 225.0   105  2.76 3.460 20.22     1     0
4  21.0     6 160.0   110  3.90 2.875 17.02     0     1
5  15.2     8 304.0   150  3.15 3.435 17.30     0     0
6  10.4     8 460.0   215  3.00 5.424 17.82     0     0
# ... with 2 more variables: gear <dbl>, carb <dbl>

so 2 samples per cylinder. That looks cool. However, there is a way to set the vector of sizes corresponding to the unique elements of the grouping, so can I get n = 1 for cars with 4 cylinders, n = 10 for cars with 6 cylinders and so on?

Thank!

+4
source share
2 answers

Do each separately and then tie them together. I assume you are already in dplyr:

bind_rows(
  mtcars %>% 
    group_by(cyl) %>%
    filter(cyl==4) %>%
    sample_n(1),
  mtcars %>% 
    group_by(cyl) %>%
    filter(cyl==6) %>%
    sample_n(6))

We cannot do 10 lines of cyl == 6, because there are only 6;)

+4
source

This is done in one piece:

require(dplyr)
require(tidyr)
require(purrr)

sample_scheme <- data_frame(cyl = c(4,6,8),
                            n = c(1,5,3))

mtcars %>% 
  nest(-cyl) %>% 
  left_join(sample_scheme, by = "cyl") %>%
  mutate(Sample = map2(data, n, sample_n)) %>% 
  unnest(Sample)
+3
source

Source: https://habr.com/ru/post/1657773/


All Articles