Public
Edited
Nov 28, 2022
Fork of BRATENAHL
Insert cell
# BRATENAHL TRAFFIC STOPS (CLEAN)
Insert cell
Insert cell
Insert cell
Insert cell
Inputs.table(dt)
Insert cell
Insert cell
totalStops = dt.count().objects()[0]["count"]
Insert cell
Insert cell
Insert cell
Insert cell
unknownRace = dt
.filter((d) => d["race"] == "U")
.count()
.objects()[0]["count"]
Insert cell
Insert cell
stopsbyrace = dt
.groupby("race")
.count()
.derive({
pct: (row) => (row.count / aq.op.sum(row.count)) * 100
})
Insert cell
Inputs.table(stopsbyrace)
Insert cell
Insert cell
racenotlisted = dt
.filter((d) => d["race"] === "")
.groupby("dispo")
.count()
.derive({
pct: (row) => (row.count / aq.op.sum(row.count)) * 100
})
.view()
Insert cell
Insert cell
raceGrouped = dt
.groupby("race", "dispo")
.count()
.groupby("race")
.derive({
pct: (row) => row.count / aq.op.sum(row.count)
})
.orderby("race", aq.desc("pct"))
.derive({
count: aq.escape((d) => (typeof d.count == "undefined" ? 0 : d.count))
})
.pivot("dispo", "count")
Insert cell
Inputs.table(raceGrouped)
Insert cell
Insert cell
Insert cell
racearrests = dt
.filter((d) => d["dispo"] === "ARREST")
.groupby("race")
.count()
.derive({
pct: (row) => (row.count / aq.op.sum(row.count)) * 100
})
.orderby("race", aq.desc("pct"))
Insert cell
Inputs.table(racearrests)
Insert cell
Insert cell
racearrestspct = dt
.filter((d) => d["dispo"] === "ARREST")
.filter((d) => d["race"] !== "")
.groupby("race")
.count()
.derive({
pct: (row) => (row.count / aq.op.sum(row.count)) * 100
})
.orderby("race", aq.desc("pct"))
Insert cell
Insert cell
Insert cell
racecitation = dt
.filter((d) => d["dispo"] === "CITE ISSUED")
.groupby("race")
.count()
.derive({
pct: (row) => (row.count / aq.op.sum(row.count)) * 100
})
.orderby("race", aq.desc("pct"))
Insert cell
Inputs.table(racecitation)
Insert cell
racecitationpct = dt
.filter((d) => d["dispo"] === "CITE ISSUED")
.filter((d) => d["race"] !== "")
.filter((d) => d["race"] !== "U")
.groupby("race")
.count()
.derive({
pct: (row) => (row.count / aq.op.sum(row.count)) * 100
})
.orderby("race", aq.desc("pct"))
Insert cell
Insert cell
dt
.filter((d) => d.race == "A")
.derive({
consequence: (d) =>
op.includes(["ARREST", "CITE ISSUED", "CITE SRAD"], d["dispo"])
? "consequence"
: "no_consequence"
})
.orderby((d) => d.consequence)
.view()
Insert cell
Insert cell
dt
.derive({
consequence: (d) =>
op.includes(["ARREST", "CITE ISSUED", "CITE SRAD"], d["dispo"])
? "consequence"
: "no_consequence"
})
.groupby("race", "consequence")
.count()
.impute({ count: () => 0 })
.groupby("race")
.derive({
pct: (row) => row.count / aq.op.sum(row.count)
})
.orderby("race", aq.desc("pct"))
.derive({
count: aq.escape((d) => (typeof d.count == "undefined" ? 0 : d.count))
})
.pivot("consequence", "count")
.derive({
pct: (row) =>
Math.round(
(row.consequence / (row.consequence + row.no_consequence)) * 10000
) / 100
})
.view()
Insert cell
Insert cell
// Parse datetime objects and extract day of week
dtDayOfWeek = dt
.derive({ dateWithoutTime: (d) => op.replace(d["date"], " 0:00", "") })
.derive({ dateSplit: (d) => op.split(d["dateWithoutTime"], "/") })
.spread("dateSplit", { as: ["monthClean", "dateClean", "yearClean"] })
.derive({
dateTimeObject: (d) =>
// Really weird how op.datetime counts month from 0. Just, why?
op.datetime(d["yearClean"], d["monthClean"] - 1, d["dateClean"])
})
.derive({
dayOfWeekClean: aq.escape((d) =>
d["dateTimeObject"].toLocaleString("en-us", { weekday: "long" })
)
})
Insert cell
Insert cell
stops2020 = dtDayOfWeek.filter((d) => d.yearClean === "2020")
Insert cell
Insert cell
totalStops2020 = stops2020.count().objects()[0]["count"]
Insert cell
Insert cell
stopsbyrace20 = stops2020
.groupby("race")
.count()
// .groupby("race")
.derive({
pct: (row) => row.count / aq.op.sum(row.count)
})
.orderby("race", aq.desc("pct"))
Insert cell
Inputs.table(stopsbyrace20)
Insert cell
Insert cell
stops2021 = dtDayOfWeek.filter((d) => d.yearClean === "2021")
Insert cell
totalStops2021 = stops2021.count().objects()[0]["count"]
Insert cell
Insert cell
stopsbyrace21 = stops2021
.groupby("race")
.count()
// .groupby("race")
.derive({
pct: (row) => row.count / aq.op.sum(row.count)
})
.orderby("race", aq.desc("pct"))
Insert cell
Inputs.table(stopsbyrace21)
Insert cell
Insert cell
racedispoGrouped21 = stops2021
.groupby("race", "dispo")
.count()
.groupby("race")
.derive({
pct: (row) => row.count / aq.op.sum(row.count)
})
.orderby("race", aq.desc("pct"))
.derive({
count: aq.escape((d) => (typeof d.count == "undefined" ? 0 : d.count))
})
.pivot("dispo", "count")
Insert cell
Inputs.table(dtDayOfWeek)
Insert cell
Insert cell
raceGrouped
.derive({
consequences: (d) => d.ARREST + d["CITE ISSUED"] + d["CITE SRAD"]
})
.view()
Insert cell
Inputs.table(racearrestspct)
Insert cell
Insert cell
Inputs.table(racecitationpct)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
stops = aq.from(
await FileAttachment("Jan 1 2020- Sept 15 2022 .xls - callracesex.csv").csv()
)
Insert cell
jan12020Sept152022XlsCallracesex = FileAttachment("Jan 1 2020- Sept 15 2022 .xls - callracesex.csv").csv()
Insert cell
import { addTooltips } from "@mkfreeman/plot-tooltip"
Insert cell
import { Plot } from "@mkfreeman/plot-tooltip"
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