【发布时间】:2017-01-31 10:45:59
【问题描述】:
我正在尝试使用以下变量 School(唯一)将两个数据集与需要教师的课程进行匹配。有的老师有一种专长,有的有不止一种。我一直在尝试使用 match() 和 which( %in% ) 基本函数,但我无法让它搜索所有可能的教师匹配项。它总是在第一场比赛后停止。以下是一些示例数据:
class<-c("english","history","art","art","math","history","art")
school<-c("C.H.S.","B.H.S.","D.H.S.","A.H.S.","Z.H.S.","M.H.S.","L.H.S.")
specialty<-c("math","history","English","history","literature","art","English")
teacher<-c("Jill","Jill","Sam","Liz","Liz","Liz","Rob")
teacher.skills<-data.frame(teacher, specialty)
school.needs<-data.frame(school,class)
teacher.match<-data.frame(Jill,Sam,Rob,Liz)
最终结果如下所示:
Jill<-c("No","Yes","No","No","Yes","Yes","No")
Sam<-c("Yes","No","No","No","No","No","No")
Liz<-c("No","Yes","Yes","Yes","No","Yes","Yes")
Rob<-c("Yes","No","No","No","No","No","No")
match.result<-data.frame(school.needs, teacher.match)
match.result
我什至尝试过使用这样的小功能,但仍然无法正确设置最终格式。
source.1<-school.needs
source.2<-teacher.skills
dist.name<-adist(source.1$class, source.2$specialty, partial = FALSE, ignore.case = TRUE)
min.name<-apply(dist.name, 1, min)
school.teacher.match<-NULL
for(i in 1:nrow(dist.name))
{
skills.ref<-match(min.name[i], dist.name[i,])
school.ref<-i
school.teacher.match<-rbind(data.frame(skills.ref=skills.ref, school.ref=school.ref, Teacher=source.2[skills.ref,]$teacher, Class=source.1[school.ref,]$class, School=source.1[school.ref,]$school, adist=min.name[i]), school.teacher.match)
school.teacher.match<-subset(school.teacher.match, school.teacher.match$adist==0)
}
school.teacher.match
任何帮助将不胜感激,谢谢!
【问题讨论】:
标签: r function matching string-matching