{
if (get_data == 1) {
let state_names_and_abbreviations = await FileAttachment(
"state_names_and_abbreviations.csv"
).csv();
let name_to_abbr = d3.rollup(
state_names_and_abbreviations,
(a) => a[0],
(o) => o.name
);
let gun_ownership_by_state = await FileAttachment(
"gun_ownership_by_state.csv"
).csv({ typed: true });
let gun_deaths_by_state = await FileAttachment(
"gun_deaths_by_state.csv"
).csv();
gun_deaths_by_state = d3.rollup(
gun_deaths_by_state.filter((o) => o.YEAR == "2021"),
(a) => a[0],
(o) => o.STATE
);
let data = gun_ownership_by_state.map(function (o) {
let abbr = name_to_abbr.get(o.state).abbr;
let death_rate = gun_deaths_by_state.get(abbr).RATE;
return {
abbr,
state: o.state,
ownership_percentage: o.gunOwnershipPercentage,
death_rate
};
});
return data;
} else {
return md`The raw data comes in a couple of different files from different sites, as described above. This code takes those raw files and generates the simplified file that powers the visualization. Hit the "Get data" button above exactly once, if you want to regenerate the data.`;
}
}