{
const q = .01
households.sort((a,b) => a['Total_CO2'] - b['Total_CO2'])
const thresholds = d3.range(q, 1 + q, q).map(d => +(d).toFixed(2))
const indeces = [0, ...thresholds.map(x => d3.quantileIndex(allHouseholds, x))].slice(0, 100)
const results = []
for (let i = 1; i < indeces.length; i++) {
const slice = households.slice(indeces[i - 1], indeces[i])
const avgCO2 = d3.mean(slice, d => d.Total_CO2)
const avgExp = d3.mean(slice, d => d.TOTEXP)
const avgIncome = d3.mean(slice, d => d.FINCBTAX_M)
const goodsValues = {}
for (let key of goodsMultipliers.keys()) {
goodsValues[key] = +(d3.mean(slice, d => d[key]))?.toFixed(3)
}
const {Water, ...restProps} = goodsValues
results.push({quantile: +(i * q).toFixed(2), avgCO2: +(avgCO2).toFixed(3), avgIncome: +(avgIncome).toFixed(3), avgExp: +(avgExp).toFixed(3), ...restProps})
}
return [...results, top1]
}