Public
Edited
Apr 1, 2024
Importers
Insert cell
Insert cell
Insert cell
xlsx = import("https://esm.sh/xlsx/xlsx.mjs")
Insert cell
dataRaw = xlsx.utils.sheet_to_json(
xlsx.read(await FileAttachment("DataForTable2.1.xls").arrayBuffer()).Sheets[
"Sheet1"
]
)
Insert cell
Insert cell
import { codeToName, nameToCode, fixCode } from "@huw/iso-3166-1"
Insert cell
dataCountryCodes = dataRaw.map((d) => ({
...d,
"Country code": nameToCode[d["Country name"]],
"Country name": codeToName[nameToCode[d["Country name"]]]
}))
Insert cell
Insert cell
dataCountryCodes.filter((d) => !d["Country code"]).length === 0
Insert cell
Insert cell
import { dataBasic as population } from "@huw/wpp"
Insert cell
data = aq
.from(dataCountryCodes)
.join(
aq.from(population),
(a, b) => a["Country code"] === b["Code"] && a["year"] === b["Year"],
[aq.all(), "Population"]
)
// Rename and reorder columns to taste
.select({
"Country name": "Name",
"Country code": "Code",
year: "Year",
"Life Ladder": "Cantril Ladder Score",
"Positive affect": "Positive Affect",
"Negative affect": "Negative Affect",
Population: "Population",
"Log GDP per capita": "GDP per Capita (Log)",
"Healthy life expectancy at birth": "Healthy Life Expectancy",
"Social support": "Social Support Score",
"Freedom to make life choices": "Freedom to Make Life Choices Score",
Generosity: "Generosity Score",
"Perceptions of corruption": "Perceptions of Corruption Score"
})
.objects()
Insert cell
data
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
year = 2023
Insert cell
dataSnapshot = data.filter((d) => d.Year === year)
Insert cell
import { countries110m as countries } from "@visionscarto/geo"
Insert cell
dataSnapshotGeo = ({
...countries,
features: countries.features.map((d1) => ({
...d1,
properties: {
...dataSnapshot.find((d2) => d2["Code"] === fixCode(d1.properties.a3)),
Name: codeToName[fixCode(d1.properties.a3)]
}
}))
})
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