I have a 30 x 2 data frame (df) with one column containing the names of 30 individuals and the second column containing their ID#. I want to create a function in R that randomly and most evenly splits the 30 individuals into groups and can handle division with and without remainders.
To clarify, this function would:
• Take 2 parameters as arguments: the df and an integer representing the number of groups • Give me back the original df but with an additional column having the group number that each person gets assigned to randomly • If the number of people (rows) cannot be divided by the integer given, the remaining rows should be split as evenly as possible between the groups
For example: • If I want the 30 people split into 1 group, my function should return df with a new column "group_no" that has 1 for every person (each person would be assigned to the same group)
• If I want 4 groups, I want to see 10 people assigned to 2 groups and the remaining 5 people assigned to another 2 groups.
• If I want 8 groups, then the function should give me 6 groups of 4 people and 2 groups of 3 and so on.
I've written some code that kind of does what I need but I'm just manually entering the groups so not just how random or correct it is... I want to instead write all this in a function that can automatically perform these tasks:
#My code so far
#For 1 group of 30 people
people=1:30
groups=1
df$group_no <- print(sample(groups))
#For 4 groups (2 groups of 10 people and 2 groups of 5 people)
groups=c(rep(1,5), rep(2,5), rep(3,10), rep(4,10))
df$group_no <- print(sample(groups))
#For 7 groups (3 groups of 6 people and 4 groups of 3 people)
groups=c(rep(1,6), rep(2,6), rep(3,6), rep(4,3), rep(5,3), rep(6,3), rep(7,3))
df$group_no <- print(sample(groups))
#For 8 groups (6 groups of 4 people and 2 groups of 3 people)
groups=c(rep(1,4), rep(2,4), rep(3,4), rep(4,4), rep(5,4), rep(6,4), rep(7,3), rep(8,3))
df$group_no <- print(sample(groups))
#For 10 groups of 3 people each
groups=c(rep(1,3), rep(2,3), rep(3,3), rep(4,3), rep(5,3), rep(6,3), rep(7,3), rep(8,3), rep(9,3), rep(10,3))
df$group_no <- print(sample(groups))
fct_grouping <- function(df, nr_groups) {
?????
}