Public
Edited
Oct 15, 2024
Insert cell
Insert cell
Insert cell
Insert cell
beforeEnd = data.filter((d) => d.percentile == 90 && d.avg_welfare < 0).map((d) => {
d.year = "" + d.year;
return d;
});
Insert cell
afterEnd = data.filter((d) => d.percentile == 90 && d.avg_welfare > 0).map((d) => {
d.year = "" + d.year;
return d;
});
Insert cell
after = data.filter((d) => d.avg_welfare > 0)
Insert cell
before = data.filter((d) => d.avg_welfare < 0)
Insert cell
order = d3.groupSort(data,
(g) => d3.max(g, (d) => d["intermediate-region"]),
(d) => d['country_code']
).reverse()
Insert cell
annualDiff = calculateAnnualChange(diff, diffYears);
Insert cell
diff = sumDiffArrays(data.reduce((acc, item) => {
const { country_code, avg_welfare, percentile } = item;
if (percentile == 50) {
acc[country_code] = acc[country_code] || [];
acc[country_code].push(avg_welfare);
}
return acc;
}, {}));
Insert cell
diffYears = calculateYearDiff(data.reduce((acc, item) => {
const { country_code, percentile, year } = item;
if (percentile == 50) {
acc[country_code] = acc[country_code] || [];
acc[country_code].push(year);
}
return acc;
}, {}));
Insert cell
medians = data.filter((d) => {
if (d['percentile'] == 50) {
d.median = d.avg_welfare;
return d;
}
})
Insert cell
data = africa_yearly_income_ppp_countries3.map((d) => {
if (d["intermediate-region"] == "") {
d["intermediate-region"] = "North Africa";
}
return d;
})
Insert cell
function calculateAnnualChange(diff, diffYears) {
const result = [];

for (let i = 0; i < diff.length; i++) {
const countryCode = diff[i].country_code;
const value = diff[i].value;
const yearDiff = diffYears.find(item => item.country_code === countryCode).year_diff;
const annualChange = value / yearDiff;
result.push({ country_code: countryCode, annual_change: annualChange });
}

return result;
}
Insert cell
function calculateYearDiff(diffYears) {
const result = [];

for (const countryCode in diffYears) {
const years = diffYears[countryCode];
const yearDiff = years[1] - years[0];
result.push({ country_code: countryCode, year_diff: yearDiff });
}

return result;
}
Insert cell
function sumDiffArrays(diff) {
const result = [];

for (const countryCode in diff) {
const array = diff[countryCode];
const sum = array.reduce((acc, val) => acc + val, 0);
result.push({ country_code: countryCode, value: sum });
}

return result;
}
Insert cell
africa_yearly_income_ppp_countries@3.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

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