gap <- read.csv("gapminder-FiveYearData.csv", header = TRUE)
The gapminder
data set has 1704
rows and 6
columns: country, year, pop, continent, lifeExp, gdpPercap
.
The country
column has 142
unique countries.
The year
column has 12
unique years: 1952, 1957, 1962, 1967, 1972, 1977, 1982, 1987, 1992, 1997, 2002, 2007
.
The range of population listed in the pop
column is between 60011
and 1318683096
.
The continent
column has 5
unique continents.
The range of life expectancy listed in the lifeExp
column is between 23.599
and 82.603
years.
The range of life GDP per capital listed in the gdpPercap
column is between 241.1659
and 113523.1
.
i <- 1
df <- matrix(nrow = 5, ncol = 2)
for( iContinent in unique(gap$continent) ){
df[i,1] <- iContinent
df[i,2] <- sd(subset(gap, continent==iContinent)$lifeExp)
i <- i+1
}
df <- as.data.frame(df)
df$V1 <- as.character(df$V1)
df$V2 <- as.numeric(as.character(df$V2))
df[order(df$V2, decreasing = TRUE),] # Rank by standard deviation
## V1 V2
## 1 Asia 11.864532
## 4 Americas 9.345088
## 3 Africa 9.150210
## 2 Europe 5.433178
## 5 Oceania 3.795611
The Asia
continent has the largest standard deviation of life expectancy of 11.8645315
.
lowerThreshold <- 50
upperThreshold <- 70
for( iCountry in unique(gap$country) ){
tmp <- mean(subset(gap, country==iCountry)$lifeExp)
if(tmp < lowerThreshold){
cat("Average Life Expectancy in", iCountry, "is less than", lowerThreshold, "\n")
}
else if(tmp > lowerThreshold && tmp < upperThreshold){
cat("Average Life Expectancy in", iCountry, "is between", lowerThreshold, "and", upperThreshold, "\n")
}
else{
cat("Average Life Expectancy in", iCountry, "is greater than", upperThreshold, "\n")
}
rm(tmp)
}
## Average Life Expectancy in Afghanistan is less than 50
## Average Life Expectancy in Albania is between 50 and 70
## Average Life Expectancy in Algeria is between 50 and 70
## Average Life Expectancy in Angola is less than 50
## Average Life Expectancy in Argentina is between 50 and 70
## Average Life Expectancy in Australia is greater than 70
## Average Life Expectancy in Austria is greater than 70
## Average Life Expectancy in Bahrain is between 50 and 70
## Average Life Expectancy in Bangladesh is less than 50
## Average Life Expectancy in Belgium is greater than 70
## Average Life Expectancy in Benin is less than 50
## Average Life Expectancy in Bolivia is between 50 and 70
## Average Life Expectancy in Bosnia and Herzegovina is between 50 and 70
## Average Life Expectancy in Botswana is between 50 and 70
## Average Life Expectancy in Brazil is between 50 and 70
## Average Life Expectancy in Bulgaria is between 50 and 70
## Average Life Expectancy in Burkina Faso is less than 50
## Average Life Expectancy in Burundi is less than 50
## Average Life Expectancy in Cambodia is less than 50
## Average Life Expectancy in Cameroon is less than 50
## Average Life Expectancy in Canada is greater than 70
## Average Life Expectancy in Central African Republic is less than 50
## Average Life Expectancy in Chad is less than 50
## Average Life Expectancy in Chile is between 50 and 70
## Average Life Expectancy in China is between 50 and 70
## Average Life Expectancy in Colombia is between 50 and 70
## Average Life Expectancy in Comoros is between 50 and 70
## Average Life Expectancy in Congo Dem. Rep. is less than 50
## Average Life Expectancy in Congo Rep. is between 50 and 70
## Average Life Expectancy in Costa Rica is greater than 70
## Average Life Expectancy in Cote d'Ivoire is less than 50
## Average Life Expectancy in Croatia is greater than 70
## Average Life Expectancy in Cuba is greater than 70
## Average Life Expectancy in Czech Republic is greater than 70
## Average Life Expectancy in Denmark is greater than 70
## Average Life Expectancy in Djibouti is less than 50
## Average Life Expectancy in Dominican Republic is between 50 and 70
## Average Life Expectancy in Ecuador is between 50 and 70
## Average Life Expectancy in Egypt is between 50 and 70
## Average Life Expectancy in El Salvador is between 50 and 70
## Average Life Expectancy in Equatorial Guinea is less than 50
## Average Life Expectancy in Eritrea is less than 50
## Average Life Expectancy in Ethiopia is less than 50
## Average Life Expectancy in Finland is greater than 70
## Average Life Expectancy in France is greater than 70
## Average Life Expectancy in Gabon is between 50 and 70
## Average Life Expectancy in Gambia is less than 50
## Average Life Expectancy in Germany is greater than 70
## Average Life Expectancy in Ghana is between 50 and 70
## Average Life Expectancy in Greece is greater than 70
## Average Life Expectancy in Guatemala is between 50 and 70
## Average Life Expectancy in Guinea is less than 50
## Average Life Expectancy in Guinea-Bissau is less than 50
## Average Life Expectancy in Haiti is between 50 and 70
## Average Life Expectancy in Honduras is between 50 and 70
## Average Life Expectancy in Hong Kong China is greater than 70
## Average Life Expectancy in Hungary is between 50 and 70
## Average Life Expectancy in Iceland is greater than 70
## Average Life Expectancy in India is between 50 and 70
## Average Life Expectancy in Indonesia is between 50 and 70
## Average Life Expectancy in Iran is between 50 and 70
## Average Life Expectancy in Iraq is between 50 and 70
## Average Life Expectancy in Ireland is greater than 70
## Average Life Expectancy in Israel is greater than 70
## Average Life Expectancy in Italy is greater than 70
## Average Life Expectancy in Jamaica is between 50 and 70
## Average Life Expectancy in Japan is greater than 70
## Average Life Expectancy in Jordan is between 50 and 70
## Average Life Expectancy in Kenya is between 50 and 70
## Average Life Expectancy in Korea Dem. Rep. is between 50 and 70
## Average Life Expectancy in Korea Rep. is between 50 and 70
## Average Life Expectancy in Kuwait is between 50 and 70
## Average Life Expectancy in Lebanon is between 50 and 70
## Average Life Expectancy in Lesotho is between 50 and 70
## Average Life Expectancy in Liberia is less than 50
## Average Life Expectancy in Libya is between 50 and 70
## Average Life Expectancy in Madagascar is less than 50
## Average Life Expectancy in Malawi is less than 50
## Average Life Expectancy in Malaysia is between 50 and 70
## Average Life Expectancy in Mali is less than 50
## Average Life Expectancy in Mauritania is between 50 and 70
## Average Life Expectancy in Mauritius is between 50 and 70
## Average Life Expectancy in Mexico is between 50 and 70
## Average Life Expectancy in Mongolia is between 50 and 70
## Average Life Expectancy in Montenegro is greater than 70
## Average Life Expectancy in Morocco is between 50 and 70
## Average Life Expectancy in Mozambique is less than 50
## Average Life Expectancy in Myanmar is between 50 and 70
## Average Life Expectancy in Namibia is between 50 and 70
## Average Life Expectancy in Nepal is less than 50
## Average Life Expectancy in Netherlands is greater than 70
## Average Life Expectancy in New Zealand is greater than 70
## Average Life Expectancy in Nicaragua is between 50 and 70
## Average Life Expectancy in Niger is less than 50
## Average Life Expectancy in Nigeria is less than 50
## Average Life Expectancy in Norway is greater than 70
## Average Life Expectancy in Oman is between 50 and 70
## Average Life Expectancy in Pakistan is between 50 and 70
## Average Life Expectancy in Panama is between 50 and 70
## Average Life Expectancy in Paraguay is between 50 and 70
## Average Life Expectancy in Peru is between 50 and 70
## Average Life Expectancy in Philippines is between 50 and 70
## Average Life Expectancy in Poland is greater than 70
## Average Life Expectancy in Portugal is greater than 70
## Average Life Expectancy in Puerto Rico is greater than 70
## Average Life Expectancy in Reunion is between 50 and 70
## Average Life Expectancy in Romania is between 50 and 70
## Average Life Expectancy in Rwanda is less than 50
## Average Life Expectancy in Sao Tome and Principe is between 50 and 70
## Average Life Expectancy in Saudi Arabia is between 50 and 70
## Average Life Expectancy in Senegal is between 50 and 70
## Average Life Expectancy in Serbia is between 50 and 70
## Average Life Expectancy in Sierra Leone is less than 50
## Average Life Expectancy in Singapore is greater than 70
## Average Life Expectancy in Slovak Republic is greater than 70
## Average Life Expectancy in Slovenia is greater than 70
## Average Life Expectancy in Somalia is less than 50
## Average Life Expectancy in South Africa is between 50 and 70
## Average Life Expectancy in Spain is greater than 70
## Average Life Expectancy in Sri Lanka is between 50 and 70
## Average Life Expectancy in Sudan is less than 50
## Average Life Expectancy in Swaziland is less than 50
## Average Life Expectancy in Sweden is greater than 70
## Average Life Expectancy in Switzerland is greater than 70
## Average Life Expectancy in Syria is between 50 and 70
## Average Life Expectancy in Taiwan is greater than 70
## Average Life Expectancy in Tanzania is less than 50
## Average Life Expectancy in Thailand is between 50 and 70
## Average Life Expectancy in Togo is between 50 and 70
## Average Life Expectancy in Trinidad and Tobago is between 50 and 70
## Average Life Expectancy in Tunisia is between 50 and 70
## Average Life Expectancy in Turkey is between 50 and 70
## Average Life Expectancy in Uganda is less than 50
## Average Life Expectancy in United Kingdom is greater than 70
## Average Life Expectancy in United States is greater than 70
## Average Life Expectancy in Uruguay is greater than 70
## Average Life Expectancy in Venezuela is between 50 and 70
## Average Life Expectancy in Vietnam is between 50 and 70
## Average Life Expectancy in West Bank and Gaza is between 50 and 70
## Average Life Expectancy in Yemen Rep. is less than 50
## Average Life Expectancy in Zambia is less than 50
## Average Life Expectancy in Zimbabwe is between 50 and 70
lowerThreshold <- 50
upperThreshold <- 70
low <- c()
medium <- c()
high <- c()
for( iCountry in unique(gap$country) ){
tmp <- mean(subset(gap, country==iCountry)$lifeExp)
if(tmp < lowerThreshold){
low <- c(low, iCountry)
}
else if(tmp > lowerThreshold && tmp < upperThreshold){
medium <- c(medium, iCountry)
}
else{
high <- c(high, iCountry)
}
rm(tmp)
}
Countiries that have average life expectancy less than 50
:
Afghanistan, Angola, Bangladesh, Benin, Burkina Faso, Burundi, Cambodia, Cameroon, Central African Republic, Chad, Congo Dem. Rep., Cote d'Ivoire, Djibouti, Equatorial Guinea, Eritrea, Ethiopia, Gambia, Guinea, Guinea-Bissau, Liberia, Madagascar, Malawi, Mali, Mozambique, Nepal, Niger, Nigeria, Rwanda, Sierra Leone, Somalia, Sudan, Swaziland, Tanzania, Uganda, Yemen Rep., Zambia
Countiries that have average life expectancy between 50
and 70
:
Albania, Algeria, Argentina, Bahrain, Bolivia, Bosnia and Herzegovina, Botswana, Brazil, Bulgaria, Chile, China, Colombia, Comoros, Congo Rep., Dominican Republic, Ecuador, Egypt, El Salvador, Gabon, Ghana, Guatemala, Haiti, Honduras, Hungary, India, Indonesia, Iran, Iraq, Jamaica, Jordan, Kenya, Korea Dem. Rep., Korea Rep., Kuwait, Lebanon, Lesotho, Libya, Malaysia, Mauritania, Mauritius, Mexico, Mongolia, Morocco, Myanmar, Namibia, Nicaragua, Oman, Pakistan, Panama, Paraguay, Peru, Philippines, Reunion, Romania, Sao Tome and Principe, Saudi Arabia, Senegal, Serbia, South Africa, Sri Lanka, Syria, Thailand, Togo, Trinidad and Tobago, Tunisia, Turkey, Venezuela, Vietnam, West Bank and Gaza, Zimbabwe
Countiries that have average life expectancy bigger than 70
:
Australia, Austria, Belgium, Canada, Costa Rica, Croatia, Cuba, Czech Republic, Denmark, Finland, France, Germany, Greece, Hong Kong China, Iceland, Ireland, Israel, Italy, Japan, Montenegro, Netherlands, New Zealand, Norway, Poland, Portugal, Puerto Rico, Singapore, Slovak Republic, Slovenia, Spain, Sweden, Switzerland, Taiwan, United Kingdom, United States, Uruguay
thresholdValue <- 50
candidateCountries <- grep("^B", unique(gap$country), value=TRUE)
par(mfrow=c(2,2))
color = c("red","blue","darkgreen","purple")
i <- 1
for( iCountry in candidateCountries){
tmp <- mean(subset(gap, country==iCountry)$lifeExp)
if(tmp < thresholdValue){
cat("Average Life Expectancy in", iCountry, "is under", thresholdValue, "plotting life expectancy graph... \n")
with(subset(gap, country==iCountry),
plot(year,lifeExp, type="o",main = paste(iCountry), ylab = "Life Expectancy", xlab = "Year", col = color[i])
) # end with
i <- i+1
} # end for loop
rm(tmp)
}
## Average Life Expectancy in Bangladesh is under 50 plotting life expectancy graph...
## Average Life Expectancy in Benin is under 50 plotting life expectancy graph...
## Average Life Expectancy in Burkina Faso is under 50 plotting life expectancy graph...
## Average Life Expectancy in Burundi is under 50 plotting life expectancy graph...
thresholdValue <- 50
candidateCountries <- grep("^J", unique(gap$country), value=TRUE)
par(mfrow=c(2,2))
color = c("red","blue","darkgreen","purple")
i <- 1
for( iCountry in candidateCountries){
tmp <- mean(subset(gap, country==iCountry)$lifeExp)
if(tmp > thresholdValue){
cat("Average Life Expectancy in", iCountry, "is above", thresholdValue, "plotting life expectancy graph... \n")
with(subset(gap, country==iCountry),
plot(year,lifeExp, type="o",main = paste(iCountry), ylab = "Life Expectancy", xlab = "Year", col = color[i])
) # end with
i <- i+1
} # end for loop
rm(tmp)
}
## Average Life Expectancy in Jamaica is above 50 plotting life expectancy graph...
## Average Life Expectancy in Japan is above 50 plotting life expectancy graph...
## Average Life Expectancy in Jordan is above 50 plotting life expectancy graph...
library(ggplot2)
thresholdValue <- 50
candidateCountries <- grep("^B", unique(gap$country), value=TRUE)
for( iCountry in candidateCountries){
tmp <- mean(subset(gap, country==iCountry)$lifeExp)
if(tmp < thresholdValue){
tmp2 <- subset(gap, country==iCountry)
print(ggplot(tmp2, aes(x=year, y=lifeExp)) +
geom_point() +
geom_line() +
labs(title=iCountry, xlab="year", ylab="Life Expectancy")
)
}
rm(tmp)
}