Published
Edited
Oct 17, 2019
Insert cell
md`# Grouping data Oct. 3`
Insert cell
d3 = require("d3")
Insert cell
// import basbeall player salary information
raw = d3.tsv("https://gist.githubusercontent.com/lathropd/0d63302f6c3c1014f2cd3846a23967a8/raw/9e7f666b7a2a83fb6d8e444215d7c46a0d60a62b/baseball.tsv")
Insert cell
formatted = d3.nest()
.key( d => d.POS )
.rollup( values => values.length )
.entries(raw)
.map( d => {
return {
position: d.key,
"number of players": d.value
}
})


Insert cell
import {table} from "@tmcw/tables"
Insert cell
table(formatted)
Insert cell
// this is a function that receives a list of players and returns a summary of them
function summarize(players) {
let count = players.length
let pay = players.map( d => +d["Average Pay"] )
let mean = d3.mean(pay)
return {count, mean}
}
Insert cell
summarize(raw)
Insert cell
maurice = d3.nest()
.key( d => d.POS )
.rollup( summarize )
.entries(raw)
.map( d => {
return {
position: d.key,
"number of players": d.value.count,
"average annual pay": d.value.mean
}
})

Insert cell
table(maurice)
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