我面临着一个特殊的情况,我的班上有31个学生,我想把他们分成8个小组(每个小组4个,只有一个人只有3个人除外).我试图遵循的具体规则是,如果其他学生已经合作过,任何学生都不能与其他学生合作.

我需要创建三个组集合(即每次包括8个组)三个星期,并遵循这个逻辑.手动这是一个有点复杂,我希望我可以写一个代码在R中得到想要的结果,但我不知道如何纠正以下,因为它没有给我在某些情况下的写入顺序.

 # List of student names
  students <- c("Student1", "Student2", "Student3", "Student4", "Student5", "Student6", "Student7", "Student8", "Student9", "Student10",
                "Student11", "Student12", "Student13", "Student14", "Student15", "Student16", "Student17", "Student18", "Student19", "Student20",
                "Student21", "Student22", "Student23", "Student24", "Student25", "Student26", "Student27", "Student28", "Student29", "Student30", "Student31")
  
  # Number of students
  num_students <- length(students)
  
  # Number of rounds
  num_rounds <- 3
  
  # Group size
  group_size <- 4
  
  # Create an empty list to store groups
  all_groups <- list()
  
  # Function to create groups
  create_groups <- function(students, group_size) {
    shuffled_students <- sample(students)
    groups <- split(shuffled_students, ceiling(seq_along(shuffled_students)/group_size))
    return(groups)
  }
  
  # Loop through each round
  for (round in 1:num_rounds) {
    # Create groups for the current round
    groups <- create_groups(students, group_size)
    
    # Append the groups to the list
    all_groups[[paste0("Round", round)]] <- groups
  }
  
  # Print the results
  for (round in 1:num_rounds) {
    cat("Round", round, ":\n")
    print(all_groups[[paste0("Round", round)]])
    cat("\n")
  }

一旦找到了正确的序列,有没有办法知道这是否奏效?

谢谢你的帮忙!

推荐答案

您可以使用循环方式来安排组,例如

v <- c(1:31, NA) # `NA` is just a placeholder to create matrix representations
p <- v[1:16]
q <- v[-(1:16)]
iter <- 3

fidx1 <- function(n, k) {
  seq(1, by = k, length.out = n) %% n + 1
}

fidx2 <- function(n, k) {
  p <- fidx1(n, k)
  c(p[-1], p[1])
}

res <- lapply(
  2 * seq_len(iter) - 1,
  \(k)
  matrix(
    rbind(p[fidx1(16, k)], q[fidx2(16, k + 2)]),
    4
  )
)

你就会得到

> res
[[1]]
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    2    4    6    8   10   12   14   16
[2,]   21   27   17   23   29   19   25   31
[3,]    3    5    7    9   11   13   15    1
[4,]   24   30   20   26   NA   22   28   18

[[2]]
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    2    8   14    4   10   16    6   12
[2,]   23   17   27   21   31   25   19   29
[3,]    5   11    1    7   13    3    9   15
[4,]   28   22   NA   26   20   30   24   18

[[3]]
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    2   12    6   16   10    4   14    8
[2,]   25   23   21   19   17   31   29   27
[3,]    7    1   11    5   15    9    3   13
[4,]   NA   30   28   26   24   22   20   18

其中每一列表示一个组


下面是用于判断组集之间是否存在重复项的判断器函数

nodupchecker <- function(...) {
  v <- as.list(...)
  u1 <- asplit(v[[1]], 2)
  u2 <- asplit(v[[2]], 2)
  all(lengths(outer(u1, u2, Vectorize(intersect))) <= 1)
}

我们看不到复制品,因为

> all(combn(res, 2, nodupchecker))
[1] TRUE

R相关问答推荐

如何删除R中除某些特定名称外的所有字符串?

如何动态更新selectizeInput?

如何通过Docker部署我的shiny 应用程序(多个文件)

在R中将特定列的值向右移动

使用geom_segment()对y轴排序

根据日期从参考帧中创建不同的帧

如果某些列全部为NA,则更改列

如何读取CSV的特定列时,给定标题作为向量

根据现有列的名称和字符串的存在进行变异以创建多个新列

R spatstat Minkowski Sum()返回多个边界

`夹心::vcovCL`不等于`AER::tobit`标准错误

删除数据帧中特定行号之间的每第三行和第四行

循环遍历多个变量,并将每个变量插入函数R

在r中整理图例和堆叠图的问题

整理曲线图、曲线图和点图

主题(Legend.key=Element_RECT(Fill=&Quot;White&Quot;))不起作用

R将函数参数传递给ggploy

将边列表转换为路径长度列表

修复标签重叠和ggploy内的空间

使用nls()函数的非线性模型的半正态图