i got the following assignment that i tried to solve in R:
"The students in the first column applied to the colleges. The following spots are available in the colleges:
College1: 8 spot
College2: 10 spot
College 3: 15 spot
The task is to fill up the available spots in the 3 different colleges based on the applicants’ score. The applicants with the highest scores shall enter their most preferred college. If a student couldn’t enter any college, you should mark him too. If two students have the same score and they both want to get into the same college, neither of them gets in. Instead those spots get reserved and will be distributed later by a council.
Example:
Student 25 and student 26 both have a score of 16, but if they both got in to their most preferred College1, than one of them would not have a bed. So they both end up in their second most pefered college, of course if there are still free spots. So they will both end up in College2 and there will be an empty spot in College1.
You should produce two tables. The first table should contain the name of the students, their ID and the name of the college to which they got assigned. The second table should contain the name of the 3 colleges and the number of students assigned to each college."
here is the csv that contains the data of the students I wrote the following R cycle to do the assigning of students:
College1 <- 8
College2 <- 10
College3 <- 15
students<-read.table("students.csv",header=T,sep=";",stringsAsFactors = F)
solution <- matrix(,nrow(students),3)
for (i in 1:(nrow(students)-1)){
if((students[i,3]==students[i+1,3])&(students[i,4]==students[i+1,4])){
solution[i,3]<-"Distributed later";solution[i+1,3]<-"Distributed later";
if(students[i,4]=="College1"){College1<-College1-2}
else if(students[i,4]=="College2"){College2<-College2-2}
else{College3<-College3-2}}
else if(students[i,4]=="College1"){if(College1>0){solution[i,3]<-"College1";College1<-College1-1}
}else if(students[i,4]=="College2 "){if(College2>0){solution[i,3]<-"College2";College2<-College2-1}
}else if(students[i,4]=="College3"){if(tarka>0){solution[i,3]<-"College3";College3<-College3-1}
}else if(students[i,5]=="College1"){if(College1>0){solution[i,3]<-"College1";College1<-College1-1}
}else if(students[i,5]=="College2 "){if(College2>0){solution[i,3]<-"College2";College2<-College2-1}
}else if(students[i,5]=="College3"){if(College3>0){solution[i,3]<-"College3";College3<-College3-1}
}else if(students[i,6]=="College1"){if(College1>0){solution[i,3]<-"College1";College1<-College1-1}
}else if(tanulok[i,6]=="College2 "){if(College2>0){solution[i,3]<-"College2";College2<-College2-1}
}else if(College3>0){solution[i,3]<-"College3";College3<-College3-1}}
}
}
College1
College2
College3
solution
I realize that there are multiple mistakes that i made in the code.
1: When 2 students have the same score and most preferred college, my code puts both of them to the "Distributed later" category, when in fact they should be assigned to their second most preferred college and one spot should be reserved ibn their most preferred college. I couldn't figure out how to solve this.
2: There is a big problem with my if/else if structure. The later parts of my code don't behave like how "else if" codes should behave, but they run even if my previous "if" command was in effect. Please help me fix the if/else if structure!
Aucun commentaire:
Enregistrer un commentaire