Public
Edited
Mar 5, 2023
Insert cell
Insert cell
Insert cell
// convert wide data to tidy data
// What if the penguin observations where made for each variable with a number seperately?
penguinDataLong = {
const data = penguinData;
const columns = Object.keys(penguinData[0]).slice(2, 6);
return Object.assign(
columns.flatMap((key) =>
data.map((d) => ({
species: d.species,
island: d.island,
sex: d.sex,
key,
value: d[key]
}))
)
).flat();
}
Insert cell
// arquero has a convenient `fold` method for this
penguinDataLong2 = {
const columns = Object.keys(penguinData[0]).slice(2, 6);
return aq.from(penguinData).fold(columns).view();
// .objects() // you can get the objects by replacing .view() with .objects()
}
Insert cell
Insert cell
Insert cell
// vanilla JS
penguinData
.map((d) => d.species)
.reduce((acc, e) => ((acc[e] = (acc[e] || 0) + 1), acc), {})
Insert cell
// vanilla JS with a Map
penguinData
.map((d) => d.species)
.reduce((acc, e) => acc.set(e, (acc.get(e) || 0) + 1), new Map())
Insert cell
// with d3.rollup
d3.rollup(
penguinData,
(v) => v.length,
(d) => d.species
)
Insert cell
// with Ramda
R.countBy((d) => d.species, penguinData)
Insert cell
// with Arquero
aq.from(penguinData)
.groupby("species")
.rollup({
count: op.count()
})
.view()
Insert cell
Insert cell
aq
.from(penguinDataLong)
.groupby("species", "key")
.rollup({
average: op.average("value"),
})
.view()
Insert cell
Insert cell
averagesMap = d3.rollup(
bla.filter((d) => !d.checkedDontKnow),
(a) => d3.sum(a, (d) => d.value) / a.length,
(d) => d.factor, // key1
(d) => d.actor_id
)
Insert cell
Array.from(averagesMap, ([factor, actors]) => ({
factor: factor,
...Object.fromEntries(actors) // maps each of cities.keys() to each of cities.values()
}))
Insert cell
Array.from(
d3.rollup(
bla.filter((d) => !d.checkedDontKnow),
(a) => d3.sum(a, (d) => d.value) / a.length,
(d) => d.factor, // key1
(d) => d.actor_id
),
([factor, actors]) => ({
factor: factor,
...Object.fromEntries(actors) // maps each of cities.keys() to each of cities.values()
})
)
Insert cell
Insert cell
penguinData
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