samedi 7 mars 2020

sample multiple different sample sizes using crossing and sample_n to create single df

I am attempting to sample a dataframe using sample_n. I know that sample_n usually takes a single size= argument at a time, however, I would like to sample sizes from 2 to the max # of rows in the df. Unfortunately, the code I have compiled below does not do the job.

df <- data.frame(Date = 1:15,
                 grp = rep(1:3,each = 5), 
                 frq = rep(c(3,2,4), each = 5))

data_sampled_by_stratum <- df %>%
  group_by(Date) %>%
  crossing(id = seq(500)) %>% # repeat dataframes
  group_by(id) %>%
  sample_n(size=c(2:15)) %>% 
  group_by(CLUSTER_ID,Date) %>% filter(n() > 2)



Aucun commentaire:

Enregistrer un commentaire