-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapportioning.R
84 lines (60 loc) · 1.88 KB
/
apportioning.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
mult = function(n,m){
val = m/sqrt((n+1)*(n))
return(val)
}
states_org = read.csv('statepops2010.csv',header=F,stringsAsFactors = F)
states_org = states_org[1:50,1:2]
states_org = cbind(states_org,rep(1,50))
names(states_org) = c('state','population','seats')
popjump = function(jump,statenum){
states = states_org
row.names(states) = seq(1,50)
population = as.numeric(states$population)
for (i in 1:385){
states[i+3] = as.numeric(mult(i,population))
}
statlist = unlist(states[4:388])
for (i in 1:385){
orgind = which.max(statlist)
ind = ifelse(orgind %% 50 == 0, 50, orgind %% 50)
states[ind,'seats'] = states[ind,'seats'] + 1
statlist[orgind] = 0
}
original = states[statenum,'seats']
states = states_org
row.names(states) = seq(1,50)
states[statenum,'population'] = states[statenum,'population'] + jump
population = as.numeric(states$population)
for (i in 1:385){
states[i+3] = as.numeric(mult(i,population))
}
statlist = unlist(states[4:388])
for (i in 1:385){
orgind = which.max(statlist)
ind = ifelse(orgind %% 50 == 0, 50, orgind %% 50)
states[ind,'seats'] = states[ind,'seats'] + 1
statlist[orgind] = 0
}
new = states[statenum,'seats']
diff = new - original
return(diff)
}
states = read.csv('statepops2010.csv',header=F,stringsAsFactors = F)
states = states[1:50,1:2]
states = cbind(states,rep(1,50))
names(states) = c('state','population','seats')
totals = data.frame(states['state'])
intervals = seq(-1000000,1000000,10000)
progress = 0
for (chg in intervals){
changes = vector()
for (statenum in 1:50){
progress = progress + 1
changes = c(changes,popjump(chg,statenum))
print(paste0('Progress: ',
100*round((progress / (length(intervals) * 50)),3),
'%'))
}
totals = cbind(totals,changes)
}
write.csv(totals,file='seat_loss_gain.csv')