Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- twocp<-function(x, cand1, cand2, cand3){
- output<-c(0,0,0)
- firstprefs<-x[which(x$CandidateName==cand1 & x$PrefCounted==1),]
- all_ballots<-x[which((x$BPNumber %in% firstprefs$BPNumber) & x$Formality=="Formal"),]
- ballot_id<-unique(firstprefs$BPNumber)
- for (i in 1:length(ballot_id)){
- ballot<-all_ballots[all_ballots$BPNumber==ballot_id[i] & !is.na(all_ballots$PrefCounted),]
- if (cand2 %in% ballot[,5] & cand3 %in% ballot[,5]){
- result<-ifelse(ballot$PrefCounted[ballot$CandidateName==cand2]<ballot$PrefCounted[ballot$CandidateName==cand3], 1,2)
- ifelse(!is.na(result), output[result]<-output[result]+1,output[3]<-output[3]+1)
- }
- else if (cand2 %in% ballot[,5] | cand3 %in% ballot[,5]){
- result<-ifelse(cand2 %in% ballot$CandidateName,1,2 )
- ifelse(!is.na(result), output[result]<-output[result]+1,output[3]<-output[3]+1 )
- }
- else{
- output[3]<-output[3]+1
- }
- }
- print(i)
- names(output)<-c(cand2, cand3, "Exhaust")
- print(paste("Preferences from", cand1))
- print(output)
- }
Add Comment
Please, Sign In to add comment