I want to randomly assign a vector of values, some of which are duplicates, to a smaller number of groups such that at least two and no more than four values are assigned to each group, and the same value is not assigned to the same group.
Example data:
values <- c(2499,2499,2522,2522,2522,2522,2648,2648,2652,2652,2670,2670,2689,2689,2690,2690,2693,2693,2700,2700,2706,2706,2714,2714,2730,2730,2738,2738,2740,2740,2765,2765,2768,2768,2773,2773,2783,2783,2794,2794,2798,2798,2807,2807,2812,2812,2831,2831,2831,2835,2835,2836,2836,2836,2844,2844,2844,2846,2846,2846,2883,2883,2964,2964)
groups <- 1:26
I tried:
split(values, sample(groups, length(values), repl = TRUE))
And this is close to what I want. But sometimes this results in just one value being assigned to a group, or more than four. And sometimes the same value (one of the duplicates) is assigned to the same group.
The desired output would have all the values randomly distributed across groups such that all values in each group are unique (no duplicates), and there is a minimum of two and a maximum of four values in each group.