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

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more