rm(list=ls()) knesset20=read.csv("knesset20.csv") knesset20=knesset20[1:25,c(5,3)] names(knesset20)=c("party", "votes") knesset20$pct=round(100*knesset20$votes/sum(knesset20$votes),3) knesset20 # surplus agreements # likud + benett: 1 - 6 # machnatz +Meretz: 2 - 10 # Kachlon + liberman: 5 - 8 # shas + aguda: 7 - 9 suragg=function(k){ suragg=k suragg$party=as.character(suragg$party) suragg$party[1]="likud + benett" suragg$mandats[1]=suragg$mandats[1]+suragg$mandats[6] suragg$votes[1]=suragg$votes[1]+suragg$votes[6] suragg$party[2]="machnatz + meretz" suragg$mandats[2]=suragg$mandats[2]+suragg$mandats[10] suragg$votes[2]=suragg$votes[2]+suragg$votes[10] suragg$party[5]="kachlon + liberman" suragg$mandats[5]=suragg$mandats[5]+suragg$mandats[8] suragg$votes[5]=suragg$votes[5]+suragg$votes[8] suragg$party[7]="shas + aguda" suragg$mandats[7]=suragg$mandats[7]+suragg$mandats[9] suragg$votes[7]=suragg$votes[7]+suragg$votes[9] suragg=suragg[-c(6,10,8,9),] return(suragg) } baderofer=function(kn){ sa=suragg(kn) kn$pay=kn$votes/(1+kn$mandats) sa$pay=sa$votes/(1+sa$mandats) w=which(sa$pay==max(sa$pay)) if(sa$party[w]=="likud + benett"){ if(kn$pay[kn$party=="likud"]>kn$pay[kn$party=="benett"]){ kn$mandats[kn$party=="likud"]=kn$mandats[kn$party=="likud"]+1 } if(kn$pay[kn$party=="likud"]kn$pay[kn$party=="meretz"]){ kn$mandats[kn$party=="machnatz"]=kn$mandats[kn$party=="machnatz"]+1 } if(kn$pay[kn$party=="machnatz"]kn$pay[kn$party=="liberman"]){ kn$mandats[kn$party=="kachlon"]=kn$mandats[kn$party=="kachlon"]+1 } if(kn$pay[kn$party=="kachlon"]kn$pay[kn$party=="aguda"]){ kn$mandats[kn$party=="shas"]=kn$mandats[kn$party=="shas"]+1 } if(kn$pay[kn$party=="shas"]threshold) knesset=elections[w, 1:2] # calculate moded moded=floor(sum(knesset$votes)/120) # assign mandats by moded knesset$mandats=floor(knesset$votes/moded) remained=120-sum(knesset$mandats) for (j in 1:remained){ knesset=baderofer(knesset) } knesset=knesset[,-2] return(knesset) } m325=theknesset(knesset20, 3.25) names(m325)[2]="m325" m275=theknesset(knesset20, 2.75) names(m275)[2]="m275" m2=theknesset(knesset20, 2) names(m2)[2]="m2" m1=theknesset(knesset20, 1) names(m1)[2]="m1" m0=theknesset(knesset20, 0) names(m0)[2]="m0" knesset20results=merge(knesset20, m325, all.x=TRUE) w=which(is.na(knesset20results$m325)) knesset20results$m325[w]=0 knesset20results=merge(knesset20results, m275, all.x=TRUE) w=which(is.na(knesset20results$m275)) knesset20results$m275[w]=0 knesset20results=merge(knesset20results, m2, all.x=TRUE) w=which(is.na(knesset20results$m2)) knesset20results$m2[w]=0 knesset20results=merge(knesset20results, m1, all.x=TRUE) w=which(is.na(knesset20results$m1)) knesset20results$m1[w]=0 knesset20results=merge(knesset20results, m0, all.x=TRUE) w=which(is.na(knesset20results$m0)) knesset20results$m0[w]=0 knesset20results=knesset20results[order(-knesset20results$votes),] rownames(knesset20results)=1:nrow(knesset20results) knesset20results=knesset20results[1:12,] yamin=c(1,5,6,7,8,9,11) yamin325=sum(knesset20results[yamin,"m325"]) yamin275=sum(knesset20results[yamin,"m275"]) yamin2=sum(knesset20results[yamin,"m2"]) yamin1=sum(knesset20results[yamin,"m1"]) yamin0=sum(knesset20results[c(1,5,6,7,8,9,11),"m0"]) yamin325 yamin275 yamin2 yamin1 yamin0 write.csv(knesset20results, file="knesset20results.csv")