Published
Edited
May 24, 2020
Insert cell
Insert cell
Insert cell
Insert cell
income_mapping = ({
'01': 5000,
'02': 12500,
'03': 20000,
'04': 30000,
'05': 42500,
'06': 62500,
'07': 87500,
'08': 112500,
'09': 137500,
'10': 175000,
'11': 250000
})
Insert cell
race_eth_domain = ['Asian','Hispanic','Non-Hispanic Black','Non-Hispanic White','Other']
Insert cell
buffulo = hhpub
.filter(r => r['HH_CBSA'] == 15380)
.map(r => ({
CNTTDHH:Number(r.CNTTDHH),
HHSIZE:Number(r.HHSIZE),
HHVEHCNT:Number(r.HHVEHCNT),
HHFAMINC: r.HHFAMINC,
HH_RACE: r.HH_RACE,
HH_HISP: r.HH_HISP,
HBRESDN: r.HBRESDN,
WTHHFIN: r.WTHHFIN
}))
.map(r => {
let race_eth = 'Other';
if (r.HH_HISP == '01') race_eth = 'Hispanic';
if (r.HH_RACE == '01') race_eth = 'Non-Hispanic White';
if (r.HH_RACE == '02') race_eth = 'Non-Hispanic Black';
if (r.HH_RACE == '03') race_eth = 'Asian';
return {...r, race_eth, income_k:income_mapping[r.HHFAMINC]}
});
Insert cell
Insert cell
function weighted_mean(data, weight_accessor, data_accessor) {
const weight_sum = d3.sum(data, weight_accessor)
return d3.sum(data, r => weight_accessor(r)*data_accessor(r)/weight_sum)
}
Insert cell
avg_trips = weighted_mean(buffulo, r=> r.WTHHFIN, r=> r.CNTTDHH)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
let totalWeight = d3.sum(buffulo, r=> r.WTHHFIN)
return d3.histogram()
.thresholds(race_eth_domain)
.value(r => r.race_eth)(buffulo)
.map((bin, i) => ({
name: race_eth_domain[i],
value: d3.sum(bin, r => r.WTHHFIN) * 100 / totalWeight
}))
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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