library(tidyverse)
library(gapminder)
library(knitr)
data(gapminder)
glimpse(gapminder)
## Observations: 1,704
## Variables: 6
## $ country <fct> Afghanistan, Afghanistan, Afghanistan, Afghanistan, ...
## $ continent <fct> Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia...
## $ year <int> 1952, 1957, 1962, 1967, 1972, 1977, 1982, 1987, 1992...
## $ lifeExp <dbl> 28.801, 30.332, 31.997, 34.020, 36.088, 38.438, 39.8...
## $ pop <int> 8425333, 9240934, 10267083, 11537966, 13079460, 1488...
## $ gdpPercap <dbl> 779.4453, 820.8530, 853.1007, 836.1971, 739.9811, 78...
# summary(gapminder$country)
gapminder %>%
select(country, year ) %>%
filter( country == "United States" ) %>%
group_by(year) %>%
summarize(n = n()) %>%
arrange(year)
gapminder %>%
select(continent, year ) %>%
filter( continent == "Americas" ) %>%
group_by(year) %>%
summarize(n = n()) %>%
arrange(year)
gapminder %>%
select( continent, country, year, lifeExp ) %>%
filter( continent == "Americas", country == "United States", lifeExp > 70 )
gapminder %>%
select( continent, country, year, lifeExp ) %>%
filter( continent == "Americas", country == "Honduras", lifeExp > 70 )
gapminder %>%
select( continent, country, year, lifeExp ) %>%
filter( continent == "Americas", lifeExp > 70 ) %>%
group_by(country) %>%
summarize(Ave_Age = mean(lifeExp))
gapminder %>%
filter( year == 2007, pop > 1000000000 ) %>%
group_by(country)
gapminder %>%
filter( pop > 1000000000 ) %>%
group_by(country, year) %>%
summarise(n = n()) %>%
arrange(year)
gapminder %>%
filter( year == 2007 ) %>%
group_by(country) %>%
arrange(gdpPercap)
gapminder %>%
filter( year == 1962 ) %>%
group_by(country) %>%
arrange(gdpPercap)
gapminder %>%
filter( year == 1962 ) %>%
group_by(country, continent) %>%
arrange(gdpPercap)
gapminder %>%
filter( year == 2007 ) %>%
group_by(country, continent) %>%
arrange(gdpPercap)
gapminder %>%
ggplot(aes(x = gdpPercap, y = lifeExp)) +
geom_point()
gapminder %>%
ggplot(aes(x = log(gdpPercap), y = log(lifeExp))) +
geom_point()
gapminder %>%
ggplot(aes(x = gdpPercap, y = lifeExp, color = continent)) +
geom_point()
gapminder %>%
ggplot(aes(x = log(gdpPercap), y = log(lifeExp), color = continent)) +
geom_point()
Based on the graph: * Lower GDP per capital but higher life Expectancy: Asia * Higher GDP per capital but higher life Expectancy: Europe
gapminder %>%
summarise(n = n(), mean_GDP = mean(gdpPercap), mean_Life = mean(lifeExp)) %>%
arrange(mean_GDP, mean_Life)
# Higher GDP, Higher LifeExp
gapminder %>%
filter( gdpPercap > 7215.327, lifeExp > 59.47444) %>%
group_by(continent) %>%
summarise(n =n()) %>%
arrange(n)
# Lower GDP, Higher LifeExp
gapminder %>%
filter( gdpPercap < 7215.327, lifeExp > 59.47444) %>%
group_by(continent) %>%
summarise(n =n()) %>%
arrange(n)
gapminder %>%
group_by(continent) %>%
summarise(n = n(), mean_GDP = mean(gdpPercap), mean_Life = mean(lifeExp)) %>%
arrange(mean_GDP, mean_Life)