Public
Edited
Jul 6, 2023
1 fork
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
countypres_2000-2020.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
Insert cell
total_figures = { data.map(d => {
d.id = d.year + "-" + d.state + "-" + d.county_fips+"-" + d.county_name + "-" + d.party
return d
})
const result = Object.values(data.reduce((acc, val) => {
const {id, candidatevotes, county_fips, state, state_po, county_name, office, candidate, party,totalvotes,version, mode,year} = val
acc[id] = { id,state, state_po, county_name, office, candidate, party,totalvotes,version, mode, county_fips,year,candidatevotes: (acc[id] ? acc[id].candidatevotes : 0) + candidatevotes };
return acc;
},{}));
return result
}
Insert cell
Insert cell
grouped_data = { total_figures.map(d => {
d.id = d.year + "-" + d.state + "-" + d.county_name +"-"+ d.county_fips
return d
})

const grouped = d3.groups(total_figures, d => d.id)

const newArray = []
grouped.map( d => {
newArray.push( d[1].reduce((prev, current) => (prev.candidatevotes > current.candidatevotes) ? prev : current))
})
return newArray
}
Insert cell
Insert cell
main = grouped_data.map(d => ({
...d,
america_check: d.year===2000 && d.party==="REPUBLICAN" ? true:
d.year === 2004 && d.party==="REPUBLICAN" ? true:
d.year === 2008 && d.party==="DEMOCRAT" ? true:
d.year===2012 && d.party==="DEMOCRAT" ? true:
d.year === 2016 && d.party==="REPUBLICAN" ? true:
d.year === 2020 && d.party==="DEMOCRAT" ? true:
false,
america_winner: d.year===2000? "GEORGE W. BUSH" :
d.year===2004? "GEORGE W. BUSH":
d.year===2008? "BARACK OBAMA":
d.year===2012? "BARACK OBAMA":
d.year===2016? "DONALD TRUMP":
d.year===2020? "JOSEPH R BIDEN JR":
false,
america_party: d.year===2000? "REPUBLICAN" :
d.year===2004? "REPUBLICAN":
d.year===2008? "DEMOCRAT":
d.year===2012? "DEMOCRAT":
d.year===2016? "REPUBLICAN":
d.year===2020? "DEMOCRAT":
''
})).map(({office, candidatevotes, version, mode, totalvotes, ...d}) => d);
Insert cell
Insert cell
main_by_count = {
const result = []
main.map(d => {
if (d.america_check === true) {
d.qty= 1
}

if (d.america_check !== true) {
d.qty= 0
}
return d
})
.reduce(function(res, value) {
// res ? console.log(res,value) : ''
if (!res[value.county_fips]) {
res[value.county_fips] = { id: value.id, state: value.state, county_fips: value.county_fips,state_po: value.state_po, county_name: value.county_name, qty: 0 };
result.push(res[value.county_fips])
}
res[value.county_fips].qty += value.qty;
return res;
}, {});
return result
}
Insert cell
Insert cell
main_zero = main_by_count.filter(d=>d.qty===6)
Insert cell
test = main.filter(d=>d.county_name.includes("WASHINGTON") && d.state==="PENNSYLVANIA")
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more