dimanche 15 janvier 2017

Sampling different numbers of rows by group in tidyverse

I'd like to sample rows from a data frame by group. But here's the catch, I'd like to sample a different number of records based on data from another table. Here is my reproducible data:

df <- data_frame(
  Stratum = rep(c("High","Medium","Low"), 10),
  Value = runif(30)
)

sampleGuide <- data_frame(
  Stratum = c("High","Medium","Low"),
  Surveys = c(3,2,5)
)

Here is my NONWORKING attempt

df %>% 
  left_join(sampleGuide, by = "Stratum") %>% 
  group_by(Stratum) %>% 
  sample_n(unique(Surveys)

It seems like sample_n requires the size to be a single number. Any ideas?

I'm only looking for tidyverse solutions. Extra points for purrr!




Aucun commentaire:

Enregistrer un commentaire