massivetotoro

Extracting 2CP from NSW LA results files

May 24th, 2021 (edited)
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 1.03 KB | None | 0 0
  1. twocp<-function(x, cand1, cand2, cand3){
  2.   output<-c(0,0,0)
  3.   firstprefs<-x[which(x$CandidateName==cand1 & x$PrefCounted==1),]
  4.   all_ballots<-x[which((x$BPNumber %in% firstprefs$BPNumber) & x$Formality=="Formal"),]
  5.   ballot_id<-unique(firstprefs$BPNumber)
  6.   for (i in 1:length(ballot_id)){
  7.     ballot<-all_ballots[all_ballots$BPNumber==ballot_id[i] & !is.na(all_ballots$PrefCounted),]
  8.     if (cand2 %in% ballot[,5] & cand3 %in% ballot[,5]){
  9.       result<-ifelse(ballot$PrefCounted[ballot$CandidateName==cand2]<ballot$PrefCounted[ballot$CandidateName==cand3], 1,2)
  10.       ifelse(!is.na(result), output[result]<-output[result]+1,output[3]<-output[3]+1)
  11.     }
  12.     else if (cand2 %in% ballot[,5] | cand3 %in% ballot[,5]){
  13.       result<-ifelse(cand2 %in% ballot$CandidateName,1,2 )
  14.       ifelse(!is.na(result), output[result]<-output[result]+1,output[3]<-output[3]+1 )
  15.     }
  16.     else{
  17.       output[3]<-output[3]+1
  18.     }
  19.   }
  20.   print(i)
  21.   names(output)<-c(cand2, cand3, "Exhaust")
  22.   print(paste("Preferences from", cand1))
  23.   print(output)
  24. }
Add Comment
Please, Sign In to add comment