table2 = tidy(
drug_arrests,
(items) => items.filter((d) => d.year == 2012),
select([
"narcotics",
"dangerous_drugs",
"other_felony_drug",
"total_drug",
"marijuana"
]),
mutate({
narcotics: (d) => d.narcotics / d.total_drug,
dangerous_drugs: (d) => d.dangerous_drugs / d.total_drug,
other_felony_drug: (d) => (d.other_felony_drug + d.marijuana) / d.total_drug
}),
pivotLonger({
cols: ["narcotics", "dangerous_drugs", "other_felony_drug"],
namesTo: "type",
valuesTo: "fraction of drug arrests"
}),
select(["type", "fraction of drug arrests"]),
mutate({
"fraction containing fentanyl": (d) =>
d.type == "narcotics"
? fentanyl_contam_narcotics
: d.type == "dangerous_drugs"
? fentanyl_contam_dd
: fentanyl_contam_other,
"fentanyl arrests": (d) =>
Math.round(
total_drug_arrests *
d["fraction containing fentanyl"] *
d["fraction of drug arrests"]
)
})
)