library(tidyverse) gradebook <- read_csv("2024-03-28T1049_Grades-CS&SS_490_A.csv") # random assignments into groups with leader and backup leader gradebook <- gradebook %>% filter(!Student %in% c(NA, "Points Possible", "Student, Test")) n_people_per_group <- 6 # six people per group how_many_groups <- floor(nrow(gradebook) / n_people_per_group) how_many_reps <- (nrow(gradebook) / how_many_groups) group_numbers <- rep(1:how_many_groups, length.out = nrow(gradebook)) # check to see this is going to work # should equal zero, -ve number is ok too (how_many_groups * n_people_per_group) - nrow(gradebook) new_cols <- paste0("week_", 1:10, "_discussion_group") gradebook_groups <- gradebook %>% select(Student) %>% mutate(!!!setNames(rep(NA, length(new_cols)), new_cols)) %>% mutate( across( .cols = starts_with("week"), .fns = ~sample(group_numbers) ) ) group_composition <- c(rep("member", (n_people_per_group - 2)), "deputy", "leader") draws <- as.vector(replicate(how_many_groups, sample(group_composition))) length(draws) # check to see this is going to work # should equal zero length(draws) - nrow(gradebook) if(length(draws) - nrow(gradebook) == 0){ NULL } else { draws <- c(draws, group_composition[1:abs(length(draws) - nrow(gradebook))]) } # check it again # should equal zero length(draws) - nrow(gradebook) gradebook_groups_roles_list <- NULL for(i in 2:ncol(gradebook_groups)){ col_name <- names(gradebook_groups[, c(1, i)]) new_col_name <- paste0("week_", i-1, "_discussion_role") gradebook_groups_roles <- gradebook_groups %>% select(all_of(col_name)) %>% arrange(across(all_of(col_name[2]))) %>% mutate(!!new_col_name := draws) %>% arrange(across(all_of(col_name[1]))) gradebook_groups_roles_list[[i]] <- gradebook_groups_roles } gradebook_groups_roles_tbl <- gradebook_groups_roles_list %>% compact %>% reduce(left_join, by = "Student") # check to see if each group has a leader and a deputy # which groups lack a leader? has at least a leader or deputy gradebook_groups_roles_tbl %>% select(Student, starts_with("week_1_")) %>% mutate(situation = ifelse(str_detect(week_1_discussion_role, "leader|deputy"), TRUE, FALSE)) %>% group_by(week_1_discussion_group) %>% tally(situation) %>% View gradebook_groups_roles_tbl %>% write_csv("gradebook_groups_roles.csv")