Published
Edited
Aug 3, 2022
Insert cell
Insert cell
ntile([0, 1, 2, 3, undefined, 4, 5, true, 6, null, 7, 8, NaN, 9, 10])
Insert cell
function ntile(arr, n = 4) {
const ps = d3.range(1, n).map((x) => x / n);
const pq = ps.map((x) => d3.quantile(arr, x));
return arr.map((x) => {
for (const [i, cut] of pq.entries()) {
if (!Number.isFinite(x)) {
return NaN;
}
if (x <= cut) {
return i + 1;
}
}
return n;
});
}
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