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

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