随机分配教师到学校或类似的学校? [英] randomly assign teachers to a school with dplyr or similar?

查看:184
本文介绍了随机分配教师到学校或类似的学校?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

makeRandomString< - function(pre,n = 1,length = $)$ {
randomString [i]< - paste0(pre,') 。',粘贴(样本(c(0:9,字母,LETTERS),
length,replace = TRUE),
collapse =))
}
return randomString)
}

gen.teachers< - function(n.teachers){
Teacher.ID< - makeRandomString(pre ='T',n =老师,长度= 20)
Teacher.exp< - runif(n = n.teachers,min = 1,max = 30)
Teacher.Other< - sample(c(0,1) ,替换= T,prob = c(0.5,0.5),size = n.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)
学校< - 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)
}

教师< - gen.teachers(n.teachers = n.teachers)
学校< - gen.schools (n.schools = n.schools)

对于每个人,我想分配6位教师老师得到1,只有1所学校)。我可以使用:

 教师%>%sample_n(6)
pre>

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



感谢您的帮助!

解决方案

在代码的上下文中

  sample(rep(schools $ School.ID ,每个= 6))

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


Suppose I have a data frame with 8 schools and its characteristics, and another with 48 teachers and its characteristics. I can generate some fake data with the following code:

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)

To each shool, I want to assign 6 teachers (every teacher get 1 and only 1 school). I could use:

Teachers %>% sample_n(6)

To get a list of 6 teachers assign those to a school, remove them from my original pool and keep going with a loop. My guess/hope is that there is a much easier way of doing this.

Thanks for the help!

解决方案

In the context of your code

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

gives a random sequence of schools where each school.id appears 6 times. Set Teachers$AssignedSchool to this sample and each teacher has an assigned school

这篇关于随机分配教师到学校或类似的学校?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆