Public
Edited
Jan 24
12 forks
Importers
37 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
data
Insert cell
Insert cell
Insert cell
nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Insert cell
Insert cell
evens = nums.filter(currentElement => currentElement % 2 === 0)
Insert cell
Insert cell
nums.filter(function(currentElement) {
return currentElement % 2 === 0;
})
Insert cell
Insert cell
{
const evens = [];
for (const num of nums) {
if (num % 2 === 0) {
evens.push(num);
}
}
return evens;
}
Insert cell
Insert cell
data.filter(currentElement => currentElement.type === 'H')
Insert cell
Insert cell
data.filter(currentElement => currentElement.calories < 100 && currentElement.sodium === 0)
Insert cell
Insert cell
data.filter(d => d.fat === 0 && d.sugars === 0)
Insert cell
Insert cell
favoriteBrands = new Set(['N', 'K', 'G'])
Insert cell
data.filter(d => favoriteBrands.has(d.mfr))
Insert cell
Insert cell
Insert cell
Insert cell
// write answer here
Insert cell
Insert cell
Insert cell
// write answer here
Insert cell
Insert cell
Insert cell
nums
Insert cell
Insert cell
nums.map(currentElement => currentElement * 2)
Insert cell
Insert cell
words = ['I', 'love', 'information', 'visualization']
Insert cell
wordLengths = words.map(currentElement => currentElement.length)
Insert cell
Insert cell
{
const wordLengths = [];
for (const word of words) {
wordLengths.push(word.length)
}
return wordLengths;
}
Insert cell
Insert cell
data.map(d => d.sodium)
Insert cell
Insert cell
abbreviationToBrandName = ({
'A': 'American Home Food Products',
'G': 'General Mills',
'K': 'Kelloggs',
'N': 'Nabisco',
'P': 'Post',
'Q': 'Quaker Oats',
'R': 'Ralston Purina'
})
Insert cell
Insert cell
cleanedData = data.map(currentElement => ({
name: currentElement.name,
rating: currentElement.rating,
type: currentElement.type === "H" ? "hot" : "cold",
shelf: currentElement.shelf,
brand: abbreviationToBrandName[currentElement.mfr]
}))
Insert cell
Insert cell
data.map(({name, rating, type, shelf, mfr}) => ({
name,
rating,
type: type === "H" ? "hot" : "cold",
shelf,
brand: abbreviationToBrandName[mfr]
}))
Insert cell
Insert cell
Insert cell
// write answer here
Insert cell
Insert cell
Insert cell
// write answer here
Insert cell
Insert cell
Insert cell
nums
Insert cell
nums.reduce((total, currentValue) => {
// update the accumulated value and return it
total += currentValue;
return total;
}, 0) // This 0 is the initial value for total. See what happens when you change it!
Insert cell
Insert cell
cleanedData.reduce((accumulator, currentValue) => {
/*
Get the count of cereals that we have seen for this brand so far.
If this is the first time we are seeing this brand, then
accumulator[currentValue.brand] will return undefined,
so count is given the default value of 0.
*/
const count = accumulator[currentValue.brand] ?? 0;
// update the count for this brand
accumulator[currentValue.brand] = count + 1;
return accumulator;
}, {}); // an empty object, {}, is our initial value
Insert cell
Insert cell
{
const accumulator = {};
for (const currentValue of cleanedData) {
const count = accumulator[currentValue.brand] ?? 0;
accumulator[currentValue.brand] = count + 1;
}
return accumulator;
}
Insert cell
Insert cell
Insert cell
unsortedNums = [9, 4, 5, 7, 2, 1, 10, 3, 8, 6]
Insert cell
// If you change toSorted to sort and rerun this cell,
// then notice what happens when you run the cell below
unsortedNums.toSorted((a, b) => d3.ascending(a, b))
Insert cell
unsortedNums
Insert cell
unsortedNums.toSorted(d3.descending)
Insert cell
Insert cell
cleanedData
.toSorted((a, b) => d3.descending(a.rating, b.rating))
.slice(0, 5) // get elements starting with index 0 and stopping before index 5
Insert cell
Insert cell
cleanedData
// only get Kelloggs cereals
.filter(d => d.brand === 'Kelloggs')
// sort in ascending order by rating
.sort((a, b) => d3.ascending(a.rating, b.rating))
// take the first three
.slice(0, 3)
// get the name property
.map(d => d.name)
Insert cell
Insert cell
Insert cell
// write answer here
Insert cell
Insert cell
Insert cell
// write answer here
Insert cell
Insert cell
Insert cell
d3.min(data, d => d.rating)
Insert cell
Insert cell
d3.min(data, function (d) {
return d.rating;
})
Insert cell
Insert cell
d3.min(data.map(d => d.rating))
Insert cell
Insert cell
d3.max(data, d => d.rating)
Insert cell
Insert cell
d3.extent(data, d => d.rating)
Insert cell
Insert cell
d3.sum(data, d => d.protein)
Insert cell
Insert cell
d3.median(data, d => d.calories)
Insert cell
Insert cell
d3.mean(data, d => d.calories)
Insert cell
Insert cell
ages = new Map([
['Abby', 30],
['Bob', 35],
['Chrissy', 25]
])
Insert cell
Insert cell
d3.max(ages, d => d[1])
Insert cell
d3.max(ages, ([name, age]) => age)
Insert cell
d3.max(ages.values())
Insert cell
Insert cell
// write answer here
Insert cell
Insert cell
Insert cell
// write answer here
Insert cell
Insert cell
Insert cell
// write answer here
Insert cell
Insert cell
Insert cell
// write answer here
Insert cell
Insert cell
Insert cell
brandToCereals = d3.group(cleanedData, cereal => cereal.brand)
Insert cell
Insert cell
brandToCereals.get('Nabisco')
Insert cell
Insert cell
{
const brandToCereals = new Map();
for (const currentValue of cleanedData) {
if (!brandToCereals.has(currentValue.brand)) {
brandToCereals.set(currentValue.brand, []);
}
brandToCereals.get(currentValue.brand).push(currentValue);
}
return brandToCereals;
}
Insert cell
Insert cell
d3.group(
cleanedData,
cereal => cereal.brand,
cereal => cereal.shelf
)
Insert cell
Insert cell
// write answer here
Insert cell
Insert cell
Insert cell
// write answer here
Insert cell
Insert cell
Insert cell
d3.rollup(cleanedData, group => group, cereal => cereal.brand)
Insert cell
Insert cell
d3.rollup(
// The first argument is our data.
cleanedData,
// The second argument is a reducing function that gets called on each group.
// The input to this function is an array of elements from our data that
// all belong to the same group. The output of this function is what is
// used as the values in the map.
//
// In this case, group is an array of all of the cereals that have the same brand.
// If we want to get the number of cereals for this brand, then we can return
// the length of the group.
group => group.length,
// The third argument is a key function that gets called on each element
// in our data array. The input to this function is a single data element.
// The output of this function is the key for the group that we want this
// element to be in.
//
// Since we want to group the cereals by their brand, we make this function
// return the brand for a cereal.
cereal => cereal.brand
)
Insert cell
Insert cell
d3.rollup(
cleanedData,
group => d3.mean(group, cereal => cereal.rating),
cereal => cereal.brand
)
Insert cell
Insert cell
d3.rollup(
cleanedData,
group => d3.extent(group, cereal => cereal.rating),
cereal => cereal.brand
)
Insert cell
Insert cell
d3.rollup(
cleanedData,
// group is all cereals that have the same brand and are on the same shelf
group => group.length,
// first group by brand
cereal => cereal.brand,
// then group by shelf
cereal => cereal.shelf
)
Insert cell
Insert cell
Insert cell
function myRollup(data, reduceFunc, keyFunc) {
// Create an empty map to store the output in.
const map = new Map();

// Step 1: Group the data according to keyFunc.

// Loop over the rows in the dataset.
// row is one element from the data array.
for (const row of data) {
// Call keyFunc to determine the key of the group
// that this element belongs in.
const key = keyFunc(row);

// Get the group for this key.
const group = map.get(key);

if (group === undefined) {
// If this group does not exist, then this row is the first
// element in this group. We need to create a new array for
// the group that contains this element.
map.set(key, [row]);
} else {
// If this group already exists, then we can just add
// this element to the end of the array.
group.push(row);
}
}

// Step 2: Call reduceFunc on each group
// Loop over the key-value pairs in the map.
for (const keyValuePair of map) {
const key = keyValuePair[0];
// Group is an array of elements that all belong to the same group.
const group = keyValuePair[1];

// Set the value for this key to be the output of the reduceFunc on this group.
// reduceFunc, for example, may get the length of the group or calculate an average.
map.set(key, reduceFunc(group));
}

return map;
}
Insert cell
Insert cell
myRollup(
cleanedData,
group => d3.mean(group, cereal => cereal.rating),
cereal => cereal.brand
)
Insert cell
d3.rollup(
cleanedData,
group => d3.mean(group, cereal => cereal.rating),
cereal => cereal.brand
)
Insert cell
Insert cell
function getMeanRatingOfGroup(group) {
return d3.mean(group, cereal => cereal.rating);
}
Insert cell
function getCerealBrand(cereal) {
return cereal.brand;
}
Insert cell
myRollup(cleanedData, getMeanRatingOfGroup, getCerealBrand)
Insert cell
d3.rollup(cleanedData, getMeanRatingOfGroup, getCerealBrand)
Insert cell
Insert cell
Insert cell
// write answer here
Insert cell
Insert cell
Insert cell
// write answer here
Insert cell
Insert cell
Insert cell
// write answer here
Insert cell
Insert cell
Insert cell
d3.group(cleanedData, cereal => cereal.brand)
Insert cell
d3.groups(cleanedData, cereal => cereal.brand)
Insert cell
Insert cell
d3.rollup(
cleanedData,
group => group.length,
cereal => cereal.brand
)
Insert cell
d3.rollups(
cleanedData,
group => group.length,
cereal => cereal.brand
)
Insert cell
Insert cell
brandRatings = d3.rollups(
cleanedData,
group => d3.median(group, cereal => cereal.rating),
cereal => cereal.brand
)
Insert cell
top3Brands = brandRatings
.toSorted((a, b) => d3.descending(a[1], b[1]))
.slice(0, 3)
Insert cell
Insert cell
d3.rollups(
cleanedData,
group => d3.median(group, cereal => cereal.rating),
cereal => cereal.brand
)
.sort((a, b) => d3.descending(a[1], b[1]))
.slice(0, 3)
Insert cell
Insert cell
// write answer here
Insert cell
Insert cell
Insert cell
// write answer here
Insert cell
Insert cell
Insert cell
d3.rollup(
cleanedData,
// group is all cereals that have the same brand and are on the same shelf
group => d3.mean(group, cereal => cereal.rating),
// first group by brand
cereal => cereal.brand,
// then group by shelf
cereal => cereal.shelf
)
Insert cell
d3.flatRollup(
cleanedData,
// group is all cereals that have the same brand and are on the same shelf
group => d3.mean(group, cereal => cereal.rating),
// first group by brand
cereal => cereal.brand,
// then group by shelf
cereal => cereal.shelf
)
Insert cell
Insert cell
Insert cell
d3.groupSort(
// our data
cleanedData,
// we want to sort the groups by their median rating
group => d3.median(group, cereal => cereal.rating),
// we want to group the cereals by brand
cereal => cereal.brand
)
Insert cell
Insert cell
d3.groupSort(
cleanedData,
group => d3.median(group, cereal => cereal.rating),
cereal => cereal.brand
).reverse()
Insert cell
d3.groupSort(
cleanedData,
group => -d3.median(group, cereal => cereal.rating),
cereal => cereal.brand
)
Insert cell
Insert cell
d3.rollups(
cleanedData,
group => d3.median(group, cereal => cereal.rating),
cereal => cereal.brand
)
.sort((a, b) => d3.descending(a[1], b[1]))
.slice(0, 3)
.map(([brand, rating]) => brand)
Insert cell
Insert cell
d3.groupSort(
cleanedData,
group => -d3.median(group, cereal => cereal.rating),
cereal => cereal.brand
).slice(0, 3)
Insert cell
Insert cell
// write answer here
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