This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.
life <- read.csv("lifeexpectancy.csv")
#inspecting dataset
dim(life)
## [1] 21565 4
names(life)
## [1] "Entity" "Code" "Year" "Life.expectancy"
head(life)
## Entity Code Year Life.expectancy
## 1 Afghanistan AFG 1950 28.1563
## 2 Afghanistan AFG 1951 28.5836
## 3 Afghanistan AFG 1952 29.0138
## 4 Afghanistan AFG 1953 29.4521
## 5 Afghanistan AFG 1954 29.6975
## 6 Afghanistan AFG 1955 30.3660
str(life)
## 'data.frame': 21565 obs. of 4 variables:
## $ Entity : chr "Afghanistan" "Afghanistan" "Afghanistan" "Afghanistan" ...
## $ Code : chr "AFG" "AFG" "AFG" "AFG" ...
## $ Year : int 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 ...
## $ Life.expectancy: num 28.2 28.6 29 29.5 29.7 ...
#cleaning dataset
library(dplyr)
library(countrycode)
life2023 <- life %>%
rename(
country = Entity,
code = Code,
year = Year,
life_expectancy = Life.expectancy
) %>%
filter(year == 2023) %>%
filter(!is.na(code), code != "") %>%
filter(!is.na(life_expectancy)) %>%
#here we are creating the continents for comparison using the country names which is done using 3 letter iso3c format via countrycode
mutate(
continent = countrycode(
code,
origin = "iso3c",
destination = "continent"
)
) %>%
filter(!is.na(continent)) %>%
select(country, code, year, continent, life_expectancy)
## Warning: There was 1 warning in `mutate()`.
## ℹ In argument: `continent = countrycode(code, origin = "iso3c", destination =
## "continent")`.
## Caused by warning:
## ! Some values were not matched unambiguously: OWID_AFR, OWID_ASI, OWID_EUR, OWID_HIC, OWID_KOS, OWID_LIC, OWID_LMC, OWID_OCE, OWID_UMC, OWID_WRL
## To fix unmatched values, please use the `custom_match` argument. If you think the default matching rules should be improved, please file an issue at https://github.com/vincentarelbundock/countrycode/issues
head(life2023)
## country code year continent life_expectancy
## 1 Afghanistan AFG 2023 Asia 66.0346
## 2 Albania ALB 2023 Europe 79.6019
## 3 Algeria DZA 2023 Africa 76.2610
## 4 American Samoa ASM 2023 Oceania 72.8518
## 5 Andorra AND 2023 Europe 84.0406
## 6 Angola AGO 2023 Africa 64.6170
#checking our cleaned data
nrow(life2023)
## [1] 236
table(life2023$continent)
##
## Africa Americas Asia Europe Oceania
## 58 55 51 49 23
summary(life2023$life_expectancy)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 54.46 69.11 75.30 74.13 79.63 86.37
#boxplot creation
ggplot(
life2023,
aes(
x = continent,
y = life_expectancy
)
) +
geom_boxplot(fill = "lightgreen") +
labs(
title = "Life Expectancy at Birth (Years)"
) +
theme_minimal()
#performing ANOVA then Tukey's test to evaluate hypothesis
life_anova <- aov(
life_expectancy ~ continent,
data = life2023
)
summary(life_anova)
## Df Sum Sq Mean Sq F value Pr(>F)
## continent 4 5885 1471.2 59.62 <2e-16 ***
## Residuals 231 5701 24.7
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
TukeyHSD(life_anova)
## Tukey multiple comparisons of means
## 95% family-wise confidence level
##
## Fit: aov(formula = life_expectancy ~ continent, data = life2023)
##
## $continent
## diff lwr upr p adj
## Americas-Africa 9.9621698 7.391528 12.5328121 0.0000000
## Asia-Africa 9.2684352 6.646561 11.8903098 0.0000000
## Europe-Africa 14.1340107 11.483817 16.7842043 0.0000000
## Oceania-Africa 6.7318862 3.366287 10.0974850 0.0000010
## Asia-Americas -0.6937346 -3.348855 1.9613860 0.9521247
## Europe-Americas 4.1718409 1.488752 6.8549296 0.0002689
## Oceania-Americas -3.2302836 -6.621846 0.1612786 0.0702743
## Europe-Asia 4.8655755 2.133362 7.5977889 0.0000180
## Oceania-Asia -2.5365490 -5.967106 0.8940077 0.2536141
## Oceania-Europe -7.4021245 -10.854373 -3.9498760 0.0000001