I've been working on this for a while and I can't really find any solution that accomplishes what I need...
Putting it simple, I have a DF with two columns, let's say, 'n' different rows (e.g. names of students), and 'm' different names of supervisors. 'n' can be larger or smaller than 'm'.
The problem:
Randomly assign the 'n' students into supervisor's groups 'm' such that every supervisor receives the same number of students in his group (or as close as possible if 'n' and 'm' are not multiples).
Some students are already assigned to a particular supervisor before hand. This is, some groups are empty at the beginning, and some already have some rows assigned.
The limit of rows per group is the same for every group, this is,
round(n/m).Rows 'n' that are already assigned into one group CAN'T be switched to a new group.
So far I've been trying to sort the problem with dplyr, playing with different tables, assigning indices for each observation... but I feel my code is way too complicated for this type of problem, so I'm wondering if someone knows a simpler solution.
I'll leave a sample of my data frame for visual purposes. Of course I'm dealing with a much bigger data set with different type of info. but the problem is exactly the same:
I have:
Names_stud (n) Supervisors (m)
Ralph SKINNER
Michael NA
Mitch NA
Julen NA
Richard CARAPAPEL
John NA
Ramon SKINNER
Laura McGONAGALL
Paul NA
Ivy NA
Lucas NA
Mathiew NA
What I would like to have:
Names_students Supervisor
Ralph SKINNER
Michael CARAPAPEL
Mitch SKINNER
Julen McGONAGALL
Richard CARAPAPEL
John CARAPAPEL
Ramon SKINNER
Laura McGONAGALL
Paul McGONAGALL
Ivy SKINNER
Lucas McGONAGALL
Mathiew CARAPAPEL
Such that:
table(DF$Supervisors)
McGONAGALL SKINNER CARAPAPEL
4 4 4
In case 'n' is not a multiple of 'm' is perfectly OK to have the closest result the this one (e.g. 4, 3, 3, or 4, 4, 3...).
So far I've done a lot of coding with dplyr, assigning indices to students previously assigned... but I always get stuck somewhere and I feel the way I deal with it is supper inefficient.
I am wondering if someone knows a specific solution to deal with this. I've also checked the 'split' package. Could't find anything useful for this purpose there.
Thank you very much in advance. If you need any further clarification, please just ask.
PD: I couldn't find any related question to this specific problem. If there's one with a proper answer please let me know.
Again, thank in advance.