This analysis is based on the World Bank population data for 217 countries and economies.
I chose this dataset to determine how the population is distributed across the most populous countries. I focused on the top ten countries and compared these to the other remaining countries.
library(tidyverse)
## Warning: package 'ggplot2' was built under R version 4.4.3
## Warning: package 'readr' was built under R version 4.4.3
## Warning: package 'purrr' was built under R version 4.4.3
## Warning: package 'dplyr' was built under R version 4.4.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.2.1 ✔ readr 2.2.0
## ✔ forcats 1.0.1 ✔ stringr 1.6.0
## ✔ ggplot2 4.0.3 ✔ tibble 3.3.1
## ✔ lubridate 1.9.5 ✔ tidyr 1.3.2
## ✔ purrr 1.2.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
# Upload the World Bank CSV file
POP <- read_csv("POP.csv")
## New names:
## Rows: 328 Columns: 8
## ── Column specification
## ──────────────────────────────────────────────────────── Delimiter: "," chr
## (5): ...1, Population 2025, ...4, ...5, ...6 lgl (3): ...3, ...7, ...8
## ℹ Use `spec()` to retrieve the full column specification for this data. ℹ
## Specify the column types or set `show_col_types = FALSE` to quiet this message.
## • `` -> `...1`
## • `` -> `...3`
## • `` -> `...4`
## • `` -> `...5`
## • `` -> `...6`
## • `` -> `...7`
## • `` -> `...8`
# Look at the first rows of the dataset
head(POP)
# Load a basic summary of the dataset
summary(POP)
## ...1 Population 2025 ...3 ...4
## Length:328 Length:328 Mode:logical Length:328
## Class :character Class :character NA's:328 Class :character
## Mode :character Mode :character Mode :character
## ...5 ...6 ...7 ...8
## Length:328 Length:328 Mode:logical Mode:logical
## Class :character Class :character NA's:328 NA's:328
## Mode :character Mode :character
# Count the number of missing values in the dataset
sum(is.na(POP))
## [1] 1713
I have removed all the rows and columns that were not needed for this analysis and renamed and changed the population and rank variables to numerical values.
# Remove the first four rows because they contain metadata
POP_clean <- POP[-c(1:4), ]
# Keep only the columns needed for the analysis
POP_clean <- POP_clean %>% select(...1, `Population 2025`, ...4, ...5)
# Rename the columns to make them easier to understand
POP_clean <- POP_clean %>% rename(country_code = ...1, rank = `Population 2025`, country = ...4, population_thousands = ...5)
# Convert population and rank from character values to numbers
POP_clean <- POP_clean %>% mutate(population_thousands = parse_number(population_thousands),rank = parse_number(rank))
## Warning: There was 1 warning in `mutate()`.
## ℹ In argument: `rank = parse_number(rank)`.
## Caused by warning:
## ! 2 parsing failures.
## row col expected actual
## 234 -- a number a. Excludes Abkhazia and South Ossetia. b. Excludes Transnistria.
## 236 -- a number http://data.worldbank.org/data-catalog/world-development-indicators
# Remove rows that do not have a country or population rank
POP_clean <- POP_clean %>% filter(!is.na(country), !is.na(rank))
I divided the dataset into two groups: the ten most populous countries and the remaining countries.
# Count how many countries are in the top 10 and how many are outside the top 10
POP_clean %>% count(rank <= 10)
# Group the countries into the top 10 and the remaining countries
# Then calculate the total and average population for each group
population_groups <- POP_clean %>% group_by(rank <= 10) %>% summarize(total_population = sum(population_thousands), average_population = mean(population_thousands))
# Display the population results
population_groups
# Calculate the percentage of the dataset's population represented by each group
population_groups %>% mutate(percent = total_population / sum(total_population) * 100)
The results indicate that the 10 most populated countries within the dataset represented 56.9% of the total population in the dataset, despite the making up only 10 out of the 217 countries.
The 10 countries resulted in an average population of 465.8 million, while the remaining countries had an average population of 17.1 million.
# Create a bar chart comparing total population between the two groups
ggplot(population_groups, aes(x = `rank <= 10`, y = total_population)) + geom_col() + labs(x = "Population group", y = "Population (thousands)", title = "Population of Top 10 Countries vs. Other Countries")
According to the analysis, the most populous nations made up a large share of the population in the dataset. More than half of the total population shown in the figures above came from the top ten nations.