#OPEN FILE AND ENSURE IT IS IN THE CORRECT DIRECTORY

getwd()
## [1] "C:/Users/anmol/OneDrive/Documentos"
#IMPORT PACKAGES

library(readr)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)

Guiding Question

How does the total population of the 50 highest-ranked economies compare with the remaining economies, and how many economies have populations of at least 10 million?

#1. IMPORT DATA

#REFERENCE FROM ATTACHED SHEET:
#The first five rows contain titles and blank rows

population <- read_csv(
  "C:/Users/anmol/Downloads/POP.csv",
  skip = 5, col_names = c("code","rank","blank1","economy","population","blank2", "blank3", "blank4")
)
## Rows: 324 Columns: 8
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (4): code, rank, economy, blank2
## num (1): population
## lgl (3): blank1, blank3, blank4
## 
## ℹ 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.
#KEEP ONLY THE COLUMNS NEEDED

population <- population[
  c("code", "rank", "economy", "population")
]

#REMOVE BLANK AND SUMMARY ROWS

population <- subset( population, !is.na(code) & !is.na(rank))

#CONVERT RANK FROM TEXT TO NUMBERS
#This allows R to compare the population rankings

population$rank <- as.numeric(population$rank)

#REMOVE COMMAS FROM POPULATION AND CONVERT IT TO NUMBERS
#This allows R to calculate totals and averages

population$population <- as.numeric(population$population)

#PRINT THE NUMBER OF ROWS AND COLUMNS

dim(population)
## [1] 217   4

Insight 1: Population by Ranking Group

#2. GROUP ECONOMIES BY POPULATION RANK

#REFERENCE FROM ATTACHED SHEET:
#The rank column is used to separate the top 50 economies
#from the remaining economies

population$rank_group <- ifelse(
  population$rank <= 50,
  "Top 50",
  "Ranks 51-217"
)

#USE group_by() AND summarize()

population_summary <- population %>%
  group_by(rank_group) %>%
  summarize(
    total_population = sum(population),
    average_population = mean(population)
  )

#PRINT RESULTS

population_summary
## # A tibble: 2 × 3
##   rank_group   total_population average_population
##   <chr>                   <dbl>              <dbl>
## 1 Ranks 51-217          1092082              6539.
## 2 Top 50                7100002            142000.
#A tibble: 2 × 3
#  rank_group   total_population average_population
#  <chr>                   <dbl>              <dbl>
#1 Ranks 51-217          1092082            6539.413
#2 Top 50                7100002          142000.040

The top 50 economies have a combined population of approximately 7.1 billion people. The remaining 167 economies have a combined population of approximately 1.09 billion people. This shows that most of the population is concentrated among the 50 highest-ranked economies.

Insight 2: Number of Economies by Population Size

#3. COUNT ECONOMIES BY POPULATION SIZE

#REFERENCE FROM ATTACHED SHEET:
#Population is reported in thousands
#Therefore, 10,000 represents 10 million people

population$population_group <- ifelse(
  population$population >= 10000,
  "10 million or more",
  "Under 10 million"
)

#USE count() TO COUNT THE ECONOMIES IN EACH GROUP

population_count <- population %>%
  count(population_group)

#PRINT RESULTS

population_count
## # A tibble: 2 × 2
##   population_group       n
##   <chr>              <int>
## 1 10 million or more    95
## 2 Under 10 million     122
#A tibble: 2 × 2
#  population_group      n
#  <chr>             <int>
#1 10 million or more    95
#2 Under 10 million     122

There are 95 economies with populations of at least 10 million people and 122 economies with populations below 10 million. Although more economies have fewer than 10 million people, most of the total population is concentrated among the largest economies.

Visualization

#4. CREATE A GRAPH OF THE FIRST INSIGHT

ggplot(
  population_summary,
  aes(
    x = rank_group,
    y = total_population / 1000000
  )
) +
  geom_col(fill = "steelblue") +
  labs(
    title = "Total Population by Ranking Group",
    x = "Ranking Group",
    y = "Population in Billions"
  ) +
  theme_minimal()

Conclusion: The results show that population is not evenly distributed across the ranked economies. The 50 highest-ranked economies contain most of the population, even though more individual economies have populations below 10 million.