Published
Edited
Sep 15, 2021
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
data = d3.csv(url, crash => ({
// combine the date and time strings into one Date object
dateTime: d3.timeParse('%m/%d/%Y %H:%M')(crash.date + ' ' + crash.time),
borough: crash.borough,
zip: crash.zip,
injured: +crash.injured, // + converts the string to an int
killed: +crash.killed,
cause: crash.cause
}))
Insert cell
Insert cell
Insert cell
d3.mean(data, crash => crash.killed + crash.injured)
Insert cell
Insert cell
d3.rollup(data, g => g.length, crash => crash.dateTime.getDate())
Insert cell
Insert cell
data.filter(crash => crash.zip === '').length;
Insert cell
Insert cell
d3.groups(data, crash => crash.zip).filter(crash => crash[0] !== '').length
Insert cell
Insert cell
d3.max(data, crash => crash.killed + crash.injured)
Insert cell
Insert cell
d3.groupSort(data, group => -group.length, crash => crash.borough) // or use reverse fn
Insert cell
Insert cell
data.filter(crash => crash.zip === '11201')
Insert cell
Insert cell
data.filter(crash => crash.zip === '11201').map(crash => crash.cause)
Insert cell
Insert cell
data.slice().sort((a,b) => d3.descending(a.injured, b.injured)).slice(0,5)
Insert cell
Insert cell
md`Which zip codes have the highest number of collisions? Get the top 5`
Insert cell
d3.rollups(data, group => group.length, crash => crash.zip).sort((a,b) => d3.descending(a[1],b[1])).filter(a => a[0] !== '').slice(0,5).map(a => ({
zipCode: a[0],
numberOfCollisions: a[1]
}))
Insert cell
Insert cell
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