Published
Edited
Feb 23, 2021
Importers
2 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
get_columns_set(data, ["y", /^x/])
Insert cell
get_columns_set(tab, ["y", /^x/])
Insert cell
Insert cell
recode = function(value, options) {
if (options.recoding.has(value)) return options.recoding.get(value);
if (options.hasOwnProperty("other")) return options.other;
return value;
}
Insert cell
Insert cell
{
let x = ["foo", "bar", 2, null];
let recoding = new Map([["foo", "yolo"], [2, 12], [null, "N/A"]]);
return x.map(v => recode(v, { recoding: recoding, other: "other" }));
}
Insert cell
Insert cell
batch_derive = function(table, options) {
let columns = get_columns_set(table, options.columns);

let derives = {};
columns.forEach(col => {
let new_name = col;
if (options.prefix) new_name = options.prefix + new_name;
if (options.suffix) new_name = new_name + options.suffix;
derives[new_name] = options.f(col);
});

return table.params(options).derive(derives);
}
Insert cell
batch_derive(tab, {
columns: [/^x/],
f: v => `d => op.upper(d['${v}'])`,
suffix: "_rec"
}).view()
Insert cell
Insert cell
batch_recode = function(table, options) {
let columns = get_columns_set(table, options.columns);
let recode_options = "$.recoding";
if (options.hasOwnProperty("fallback")) {
recode_options = "$.recoding, $.fallback";
}
let derives = {};
columns.forEach(col => {
let new_name = col;
if (options.prefix) new_name = options.prefix + new_name;
if (options.suffix) new_name = new_name + options.suffix;
derives[new_name] = (v =>
`(d,$) => op.recode(d['${v}'], ${recode_options})`)(col);
});
return table.params(options).derive(derives);
}
Insert cell
batch_recode(tab, {
columns: [/^x/, "y"],
recoding: new Map([["foo", "yolo"], [2, 12], ["bye", null]])
}).view()
Insert cell
Insert cell
Insert cell
Insert cell
tab
.batch_derive({
columns: [/^x/],
f: v => `d => op.upper(d['${v}'])`,
prefix: "rec_"
})
.batch_recode({
columns: /^rec/,
recoding: new Map([["FOO", "RECODED"], ["BYE", 42]])
})
.view()
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