if we have data:
| group | group_size |
|---|---|
| a | 2 |
| b | 3 |
| c | 1 |
| d | 4 |
| e | 2 |
| f | 4 |
| g | 1 |
| h | 3 |
| i | 1 |
| j | 3 |
| k | 1 |
| l | 1 |
- Code for input data:
df <- data.frame(group = c("a","b","c","d","e","f","g","h","i","j","k","l"),
group_size = c(2,3,1,4,2,4,1,3,1,3,1,1))
and I want to randomly pair or trio groups into larger groups that are all the same size how would I do this in R? For my results - I would want all of the groups to be matched up with another group randomly and have each of the 4-5 new groups be of equal size. I need to redo this every week randomly, so I want to make sure the results can be randomized everytime.
Thanks.
sample desired output:
| new_grp | members | new_grp_size |
|---|---|---|
| group 1 | a,c,e | 5 |
| group 2 | b,l,g | 5 |
| group 3 | d,k | 5 |
| group 4 | j,h | 6 |
| group 5 | i,f | 5 |
notice all groups are as close to the same size as they can get.