title: “hw1” output: html_document date: “2025-09-18” Group Members: Yaksh Bhatia (ysb19), Akshar Patel (asp335) —
library(ggplot2)
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(countrycode)
library(knitr)
library(scales)
convert m and k
text.to.num <- function(x){
xx = as.character(x)
res=rep(0, length(x))
for(i in 1:length(x)){
if(is.na(xx[i])){
res[i]=NA
next
}
if (grepl("M", xx[i], ignore.case = TRUE)) {
res[i]=as.numeric(gsub("M", "", xx[i], ignore.case = TRUE)) * 1e6
} else if (grepl("k", xx[i], ignore.case = TRUE)) {
res[i]=as.numeric(gsub("k", "", xx[i], ignore.case = TRUE)) * 1e3
} else {
res[i]=as.numeric(xx[i])
}
}
res
}
import data
life_expectancy <- read.csv('lex.csv')
lifeexp <- life_expectancy[, c("country", "X2010")]
Child_mortality <- read.csv('Child_mortality.csv')
childmort <- Child_mortality[, c("country", "X2010")]
pop <- read.csv('pop.csv')
population <- pop[,c("country", "X2010")]
population$X2010 <- text.to.num(population$X2010)
## Warning in text.to.num(population$X2010): NAs introduced by coercion
## Warning in text.to.num(population$X2010): NAs introduced by coercion
daily_income <- read.csv('mincpcap_cppp.csv')
income <- daily_income[,c("country", "X2010")]
babies_per_woman <- read.csv('children_per_woman_total_fertility.csv')
chdperwoman <- babies_per_woman[,c("country", "X2010")]
CO2_emissions_per_person <- read.csv('co2_pcap_cons.csv')
co2 <- CO2_emissions_per_person[,c("country", "X2010")]
GDP_per_capita <-read.csv('gdppercapita_us_inflation_adjusted.csv')
gdpcapita <- GDP_per_capita[,c("country", "X2010")]
gdpcapita$X2010 <- text.to.num(gdpcapita$X2010)
total_health_spending_per_person <- read.csv('total_health_spending_per_person_us.csv')
healthspend <- total_health_spending_per_person[,c("country", "X2010")]
population_density <- read.csv('population_density_per_square_km.csv')
popdensity <- population_density[,c("country", "X2010")]
popdensity$X2010 <- text.to.num(popdensity$X2010)
at_least_basic_water <- read.csv('at_least_basic_water_source_overall_access_percent.csv')
water <- at_least_basic_water[,c("country", "X2010")]
murders <- read.csv('murder_total_deaths.csv')
murder <- murders[,c("country", "X2010")]
murder$X2010 <- text.to.num(murder$X2010)
Rename the columns
names(population)[2] <- "population_2010"
names(lifeexp)[2] <- "lifeexp_2010"
names(childmort)[2] <- "childmort_2010"
names(income)[2] <- "income_2010"
names(gdpcapita)[2] <- "gdpcapita_2010"
names(chdperwoman)[2] <- "chdperwoman_2010"
names(healthspend)[2] <- "healthspend_2010"
names(co2)[2] <- "co2_2010"
names(water)[2] <- "water_2010"
names(popdensity)[2] <- "popdensity_2010"
names(murder)[2] <- "murder_2010"
merge data
df <- Reduce(
function(x, y) merge(x, y, by = "country", all = TRUE),
list(population, lifeexp, childmort, income, gdpcapita, chdperwoman, healthspend, co2, water, popdensity, murder)
)
Continent Column
df$continent <- countrycode(sourcevar = df[, "country"],
origin = "country.name",
destination = "continent")
## Warning: Some values were not matched unambiguously: Channel Islands, Kosovo
q1 <- df[, c("lifeexp_2010", "childmort_2010")]
#head (q1)
comp1 <- q1[complete.cases(q1),]
#head(comp1)
ggplot(data=comp1, mapping=aes(x=lifeexp_2010, y=childmort_2010)) + geom_point()+labs(title = "side by side bar graph of continent by the indicator", x= "Life Expectancy 2010", y = "Child Mortality 2010")
Explanation: There seems to be a negative relationship between child mortality and life expectancy, which generally shows that as the life expectancy of a particular country increases, the child mortality rates decrease. This scatterplot looks more exponential than linear.
q2 <- df[, c("country", "lifeexp_2010", "childmort_2010", "population_2010", "continent")]
comp2 <- q2[complete.cases(q2),]
ggplot (data = comp2, mapping = aes(x = lifeexp_2010, y = childmort_2010,size = population_2010, color = continent))+geom_point(alpha = 0.2)+labs(title = "side by side bar graph of continent (by size and continent)", x= "Life Expectancy 2010", y = "Child Mortality 2010")
q3 <- df[, c("lifeexp_2010")]
comp3 <- q3[complete.cases(q3)]
ggplot(data.frame(lifeexp_2010 = comp3), aes(x = lifeexp_2010)) + geom_histogram(binwidth = 2, fill = "lightgreen", color = "black") + labs(title = "Histogram of Life Expectancy in 2010", x = "Life Expectancy", y = "Count") + theme_minimal()
The distribution shown from this histogram indicates that it is left-skewed data. Most countries have high life expectancy, a very small number of countries have lower life expectancies which results the tail pulling to the left. This also means that the mean is less than the median which is less than the mode.
q4 <- df[, c("lifeexp_2010")]
comp4 <- q4[complete.cases(q4)]
ggplot(data.frame(lifeexp_2010 = comp4), aes(x = "", y = lifeexp_2010)) + geom_boxplot(fill = "skyblue", color = "black") + labs(title = "Box-Plot of Life Expectancy in 2010", y = "Life Expectancy") + theme_minimal()
As shown from this box-plot of life expectancy, there are stand alone points. The point that falls decently far outside the whiskers is the designated country Haiti with a 32.5 life expectancy, this is considered an outlier.
q5 <- df[, c("continent", "lifeexp_2010")]
comp5 <- q5[complete.cases(q5), ]
ggplot(comp5, aes(x = continent, y = lifeexp_2010)) + geom_boxplot(fill = "lightblue", color = "black") + labs(title = "Life Expectancy in 2010 by Continent", x = "continent", y = "Life Expectancy") + theme_minimal()
The boxplots are indeed somewhat similar among the five continents. All of the continents’ countries tend to have mostly high life expectancies. Africa, Americas, and Oceania all contain outliers while Asia and Europe do not have any and contain countries with more consistency in life expectancies. Life expectancies from all five continents range from mostly 60-80.
q6 <- df[, c("continent", "country")]
comp6 <- q6[complete.cases(q6),]
ggplot(comp6, aes(x = continent)) + geom_bar(fill = "red", color = "black") + labs(title = "Number of Countries per Continent", x = "continent", y = "# of countries")
As shown in this bar graph, Africa has the largest number of countries. The continent with the second largest number of countries in the data set is Americas.
df$indicator <- case_when(
df$chdperwoman_2010 > 2.0 ~ "false",
df$chdperwoman_2010 <= 2.0 ~ "true"
)
glimpse(df$indicator)
## chr [1:237] "false" "true" "false" NA "true" "false" NA "true" "false" ...
q8 <- df[, c("country", "continent", "indicator")]
comp8 <- q8[complete.cases(q8),]
ggplot(data = comp8, mapping = aes(x=continent, fill = indicator)) + geom_bar(position = "fill")+
scale_y_continuous(labels = percent) +labs( title = "side by side bar graph of continent by the indicator", x = "continents", y = "percent")
Explanation: The percentage of countries with the average number of children <=2 does not appear to be similar among these 5 continents, with Africa being around 1% while Europe is above 75%. However, the percentages from the Americas and Asia are very similar.