gpt4 book ai didi

r - r中的多重匹配函数

转载 作者:行者123 更新时间:2023-12-02 04:38:32 24 4
gpt4 key购买 nike

我正在尝试使用以下变量学校(唯一)将两个数据集与需要教师的类(class)相匹配。有些老师有一种专长,有些则不止一种。我一直在尝试使用 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

非常感谢任何帮助,谢谢!

最佳答案

请注意,我必须修改您的输入数据以将每次匹配的 "English" 更改为 "english"。数据由:

school.needs <- structure(list(school = structure(c(3L, 2L, 4L, 1L, 7L, 6L, 5L
), .Label = c("A.H.S.", "B.H.S.", "C.H.S.", "D.H.S.", "L.H.S.",
"M.H.S.", "Z.H.S."), class = "factor"), class = structure(c(2L,
3L, 1L, 1L, 4L, 3L, 1L), .Label = c("art", "english", "history",
"math"), class = "factor")), .Names = c("school", "class"), row.names = c(NA,
-7L), class = "data.frame")

teacher.skills <- structure(list(teacher = structure(c(1L, 1L, 4L, 2L, 2L, 2L,
3L), .Label = c("Jill", "Liz", "Rob", "Sam"), class = "factor"),
specialty = structure(c(5L, 3L, 2L, 3L, 4L, 1L, 2L), .Label = c("art",
"english", "history", "literature", "math"), class = "factor")), .Names = c("teacher",
"specialty"), row.names = c(NA, -7L), class = "data.frame")

使用 reshape2(或 data.table)中的 mergedcast:

library(reshape2)
## use merge to match needs to skills
m <- merge(school.needs,teacher.skills,by.x="class",by.y="specialty")
m$val <- "Yes" ## add a column for the "Yes"
## go to wide format for the final result filling NA with "No"
result <- dcast(m,school+class~teacher,value.var="val",fill="No")
## school class Jill Liz Rob Sam
##1 A.H.S. art No Yes No No
##2 B.H.S. history Yes Yes No No
##3 C.H.S. english No No Yes Yes
##4 D.H.S. art No Yes No No
##5 L.H.S. art No Yes No No
##6 M.H.S. history Yes Yes No No
##7 Z.H.S. math Yes No No No

关于r - r中的多重匹配函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39646108/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com