随机将教师分配到dplyr或类似的学校?

时间:2015-06-23 14:09:41

标签: r dplyr

假设我有一个包含8所学校及其特征的数据框,另有48所教师及其特征。我可以使用以下代码生成一些虚假数据:

library(dplyr)
library(geosphere)
set.seed(6232015)
n.schools <-8
n.teachers <- 48

  makeRandomString <- function(pre, n=1, length=12) {
  randomString <- c(1:n)                  # initialize vector
  for (i in 1:n) {
    randomString[i] <- paste0(pre,'.', paste(sample(c(0:9, letters, LETTERS),
                                                    length, replace=TRUE),
                                             collapse="")) 
  }
  return(randomString)
}

gen.teachers <- function(n.teachers){
  Teacher.ID <- makeRandomString(pre= 'T', n = n.teachers, length = 20)
  Teacher.exp <- runif(n = n.teachers, min = 1, max = 30)
  Teacher.Other <- sample(c(0,1), replace = T, prob = c(0.5, 0.5), size = n.teachers)
  Teachers <- data.frame(Teacher.ID, Teacher.exp, Teacher.Other)
  return(Teachers)
}



gen.schools <- function(n.schools){
  School.ID <- makeRandomString(pre= 'S', n = n.schools, length = 20)
  School.lat <- runif(n = n.schools, min = -2, max = 2)
  School.long <- runif(n = n.schools, min = -2, max = 2)
  Schools <- data.frame(School.ID, School.lat, School.long) %>% 
    rowwise() %>%  mutate (School.distance = distHaversine(p1 = c(School.long, School.lat),
                                                           p2 = c(0, 0), r = 3961))
  return(Schools)
}

Teachers <- gen.teachers(n.teachers = n.teachers)
Schools <- gen.schools(n.schools = n.schools)

对于每个学校,我想分配6名教师(每位教师获得1名且只有1所学校)。我可以用:

Teachers %>% sample_n(6)

要获得6名教师的名单,将其分配给学校,将其从原始池中移除并继续循环。我的猜测/希望是有一种更简单的方法。

感谢您的帮助!

1 个答案:

答案 0 :(得分:3)

在您的代码的上下文中

sample(rep(Schools$School.ID, each = 6))

给出了一个随机的学校序列,每个school.id出现6次。将教师$ AssignedSchool设置为此样本,每位教师都有一个指定的学校