Published
Edited
May 7, 2021
8 stars
Insert cell
Insert cell
Table(jsonInput)
Insert cell
Insert cell
subsetData = tidy(
jsonInput,
// filter to a few cities
filter(d => ['Austin', 'Houston', 'Dallas', 'San Antonio'].includes(d.city))
)
Insert cell
Table(subsetData)
Insert cell
Insert cell
Insert cell
Insert cell
pctData = tidy(
subsetData,

// 1. compute the total for each month and store as a property on each point
groupBy('month', mutateWithSummary({ totalMonthSales: sum('sales') })),

// 2. compute the percent of total. use rate() to safely
// handle division by zero and nully values.
mutate({ percent: rate('sales', 'totalMonthSales') })
)
Insert cell
Table(pctData)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
totalData = tidy(
jsonInput,

// for each time period, sum up the sales
groupBy('month', summarize({ overallTotalMonthSales: sum('sales') }))
)
Insert cell
Table(totalData)
Insert cell
Insert cell
overallPctData = tidy(
subsetData,
// join our totals for each month, making `overallTotalMonthSales` available on each item
innerJoin(totalData, { by: 'month' }),

// compute the percent for each item
mutate({ percent: rate('sales', 'overallTotalMonthSales') }),

// (optional) sort the data just to make it more similar to the data above for comparison purposes
arrange('month')
)
Insert cell
Table(overallPctData)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
import {
tidy,
filter,
select,
mutate,
groupBy,
mutateWithSummary,
summarize,
sum,
rate,
TMath,
innerJoin,
leftJoin,
arrange,
} from '@pbeshai/tidyjs'
Insert cell
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