0

I have seen the answer for this question but I have K groups, each of them with n[k] elements, and I want to draw for example floor(n[k] * p) samples from each of these groups.

I was thinking something like this:

df %>%
  group_by(my_group) %>%
  mutate(
    n_samples = floor(n() / 2)
  ) %>%
  sample_n(n_samples)

But that doesn't work because the argument for sample_n should be the same for every group.

I prefer solutions using dplyr or at least following the tidyverse convention.

gsmafra
  • 2,434
  • 18
  • 26
  • 2
    are you looking for the [`sample_frac`](http://dplyr.tidyverse.org/reference/sample.html) function? – bouncyball Mar 22 '18 at 17:27
  • Thanks, that was exactly what I needed for my case. I couldn't accept that as an answer though because in the general case the number of samples can be different from a fraction of the group size. – gsmafra Mar 22 '18 at 17:31

0 Answers0