Public
Edited
May 4
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
test_var_repeat = 0
Insert cell
test_var_repeat = 42
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
//Fibonacci code block
let fib = 1, fib2= 2
for (let i = 3; i < 11; i++){
fib2 = fib2+fib
fib = fib2-fib
}
return fib2
}
Insert cell
mySquareFx = x => x*x
Insert cell
mySquareFx(10)
Insert cell
Insert cell
[5+3/2, 34, 2] //list
Insert cell
({a:1, b:10, c:1000}) //dictionary
Insert cell
({first:'Haoyu', middle: '', last:'Li'}) //dictionary
Insert cell
Insert cell
Insert cell
Insert cell
data = FileAttachment("States2020.csv").csv({typed: true}) // code to get the data object
Insert cell
data.length
Insert cell
data.columns
Insert cell
Insert cell
Insert cell
data.slice(0, 3)
Insert cell
Insert cell
data[data.length-1]
Insert cell
Insert cell
// Extract population counts per state
statePopulation = data.map( (r,i,a) => r.Pop2020)
Insert cell
// Find the population change from 2010 to 2020
statePopulationGrowth = data.map(r => r.Pop2020 - r.Pop2010)
Insert cell
// add a new column into a new data table for the population growth
data_with_growth = data.map(r => ({...r, PopGrowth: r.Pop2020 - r.Pop2010}))
Insert cell
data
Insert cell
Insert cell
Insert cell
Insert cell
data_with_popdensity = data.map(r => ({...r, PopDensity2020: r.Pop2020}))
Insert cell
Insert cell
Insert cell
data.reduce((total, state, stateIndex, d) => total + state.Pop2020, 0)
Insert cell
Insert cell
{
const findMaxIndex = (iMax, state, stateIndex, arr) => {
if (arr[iMax].Pop2020 < state.Pop2020){
return stateIndex
} else {
return iMax
}
}
const stateMostPop = data[data.reduce(findMaxIndex, 0)] // find the maximum state index using the reduce function
return stateMostPop
}
Insert cell
Insert cell
data.reduce((total, state, stateIndex, d) => total + state.Pop2020 * state.IncomePerCapita, 0)
Insert cell
Insert cell
data.filter(r => r.IncomePerCapita < 31000)
Insert cell
Insert cell
data_with_growth.filter(r => r.PopGrowth < 0)
Insert cell
Insert cell
// sort a copy of the data with a comparison function
// [...data] creates a copy of data
[...data_with_growth].sort((a,b) => a.LandArea - b.LandArea)
Insert cell
Insert cell
[...data_with_popdensity].sort((a,b) => a.PopDensity2020 - b.PopDensity2020)
Insert cell
Insert cell
group = Object.groupBy(data, (state)=>Math.floor(state.IncomePerCapita / 10000))
Insert cell
Insert cell

{
let reduced_group = {}
for (let IncomeGroup in group){
// calculate mean for each group
reduced_group[IncomeGroup] = group[IncomeGroup].reduce((total, state)=> total + state.PercentCollegeGrad, 0)/group[IncomeGroup].length
}
return reduced_group
}
Insert cell
Insert cell
Insert cell
group1 = Object.groupBy(data, (state)=>Math.floor(state.LandArea / 20000))
Insert cell
{
let reduced_group1 = {}
for (let populationGroup in group1){
// calculate total population for each group
reduced_group1[populationGroup] = group1[populationGroup].reduce((total, state)=> total + state.Pop2020, 0)
}
return reduced_group1
}
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