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(tidyr)
library(readr)
library(ggplot2)

Download the a CSV file from the World Bank.

life_expectancy<-read_csv("life expectancy.csv")
## Rows: 265 Columns: 69
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (4): Country Name, Country Code, Indicator Name, Indicator Code
## dbl (65): 1960, 1961, 1962, 1963, 1964, 1965, 1966, 1967, 1968, 1969, 1970, ...
## 
## ℹ 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.

Explore the data and come up with a question to guide your analysis.

What was the average (mean) life expectancy across all countries in 2024?

Create a simple R Markdown document that formats the data.

life_2024 <- life_expectancy %>%
  select(`Country Name`, `Country Code`, `2024`) %>%
  rename(life_expectancy = `2024`) %>%
  filter(!is.na(life_expectancy))

Use dplyr’s summarize, count, and group_by functions to produce a report that describes two key insights that answer your question.

Key insight #1. Using summarize() we can see that the average life expectancy across the 264 countries/regions in the data set was ~74 years in 2024.

life_2024 %>% summarize(n_countries = n(),mean_life_expectancy = mean(life_expectancy))
## # A tibble: 1 × 2
##   n_countries mean_life_expectancy
##         <int>                <dbl>
## 1         264                 73.7

Key insight #2.

Using count() it is evident that 140 countries had a 2024 life expectancy above the global average, while 124 fell at or below it.

life_2024 %>%
  count(above_avg = life_expectancy > mean(life_expectancy))
## # A tibble: 2 × 2
##   above_avg     n
##   <lgl>     <int>
## 1 FALSE       124
## 2 TRUE        140

Use ggplot2’s functions to visualize one of your insights.

life_2024 <- life_2024 %>%
  mutate(above_avg = if_else(life_expectancy > mean(life_expectancy),
                              "Above average", "At or below average"))
ggplot(life_2024, aes(x = above_avg)) +
  geom_bar(fill = "darkgreen") +
  labs(title = "Countries Above vs. At-or-Below Average Life Expectancy (2024)",
       x = "Group",
       y = "Number of Countries")

Publish your R Markdown document to RPub’s (https://rpubs.com/) and share your link in this discussion form (Title your post the name with the question you used to guide your exploration)