Published
Edited
May 28, 2021
Importers
2 stars
Insert cell
Insert cell
// Remove special characters from columns and rename them
data = tidy(
voting,
mutate({
total_vote: (d) => clean_num(d, "Total vote"),
registration: (d) => clean_num(d, "Registration"),
voter_turnout: (d) => clean_percent(d, "Voter Turnout"),
vap_turnout: (d) => clean_percent(d, "VAP Turnout"),
invalid_votes: (d) => clean_percent(d, "Invalid votes"),
population: (d) => clean_num(d, "Population"),
voting_age_population: (d) => clean_num(d, "Voting age population"),
compulsory_voting: (d) => d["Compulsory voting"],
year: (d) => d["Year"],
election_type: (d) => d["Election type"],
country: (d) => d.Country
}),
select([
"country",
"population",
"election_type",
"total_vote",
"registration",
"voter_turnout",
"compulsory_voting",
"invalid_votes",
"voting_age_population",
"year"
])
)
Insert cell
// Sort the countries by the average voter turnout (highest to lowest)
sorted_countries = tidy(
data,
groupBy(
"country",
summarize({
mean: m("voter_turnout")
})
),
arrange(desc("mean"))
).map((d) => d.country)
Insert cell
Insert cell
// Function to remove commas and extra spaces from numeric columns
clean_num = (d, col) =>
d[col] ? +S(d[col]).replaceAll(",", "").trim().s : null
Insert cell
// Load the data
voting = FileAttachment("idea_export_40_60ae663e879af.csv").csv({ typed: true })
Insert cell
// Data wrangling functions
import {
tidy,
mutate,
select,
groupBy,
summarize,
mean,
arrange,
desc,
median as m
} from "@pbeshai/tidyjs"
Insert cell
// Function to percent signs and extra spaces from percentage columns
clean_percent = (d, col) =>
d[col] ? +S(d[col]).replaceAll("%", "").trim().s / 100 : null
Insert cell
// Load the `string` package into the variable `S`
S = require("string@3.3.3/dist/string.min.js")
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more