Search code examples
rsamplingreplicate

I am trying to form 7 groups of random number of observations from a total of 100 observations. All observations should be used


I am trying to create a list of 7 groups with 100 observations. Each group can have different number of observations. All observations should be placed in one of the 7 groups. In other words, all observations should be used.

The code I am using does not use all the observations. Is there a way that I can solve this?

times_to_sample = 7L
  NN = nrow(df)
  sample<-replicate(times_to_sample, df[sample(NN, sample(5:15, 1L)), ], simplify = FALSE)

my expected result just has to place each observation in one of the seven groups. Any help will be appreciated. Thank you!


Solution

  • Try something like this:

    group_indices <- sample(x = 1:7, size = 100, replace = TRUE)
    
    df_splitted_in_7_groups <- split(x = df, f = group_indices)