Exercises

  1. Calculate the mean gdpPercap for each country.

  2. Calculate the mean gdp for each country.

  3. Create a graph of the total population of each continent over time.

  4. For each continent, what country had the smallest population in 1952, 1972, and 2002? (google for help if you need to!)




Solutions

library(gapminder)
library(ggplot2)
library(dplyr)

# 1. Calculate the mean gdpPercap for each country.

gapminder %>% 
  group_by(country) %>%
    summarise(mean_gdpPercap = mean(gdpPercap))
## Source: local data frame [142 x 2]
## 
##        country mean_gdpPercap
## 1  Afghanistan       802.6746
## 2      Albania      3255.3666
## 3      Algeria      4426.0260
## 4       Angola      3607.1005
## 5    Argentina      8955.5538
## 6    Australia     19980.5956
## 7      Austria     20411.9163
## 8      Bahrain     18077.6639
## 9   Bangladesh       817.5588
## 10     Belgium     19900.7581
## ..         ...            ...
# 2. Calculate the mean gdp for each country.

gapminder %>% 
  mutate(gdp = gdpPercap * pop) %>%
  group_by(country) %>%
  summarise(mean_gdp = mean(gdp))
## Source: local data frame [142 x 2]
## 
##        country     mean_gdp
## 1  Afghanistan  12709647583
## 2      Albania   9094669267
## 3      Algeria  96735171261
## 4       Angola  25532681843
## 5    Argentina 266754123835
## 6    Australia 320253755823
## 7      Austria 158579002935
## 8      Bahrain   7694793798
## 9   Bangladesh  80648494456
## 10     Belgium 197371599665
## ..         ...          ...
# 3. Create a graph of the total population of each continent over time.

gapminder %>%
  group_by(continent) %>%
  summarise(total_pop = sum(pop)) %>%
ggplot(data = ., aes(x = continent, y = total_pop)) + 
    geom_bar(stat = "identity")

# Note - read the error message if you have difficulties! We can talk about why 
# we do this in class if you are curious. 

# Alternatively you could plot points: 
gapminder %>%
    group_by(continent) %>%
    summarise(total_pop = sum(pop)) %>%

ggplot(data = ., aes(x = continent, y = total_pop)) + 
    geom_point()

# 4. For each continent, what country had the smallest population in 1952, 1972, 
#    and 2002? (google for help if you need to!)

gapminder %>% 
  filter(year %in% c(1952, 1972, 2002)) %>%
  group_by(continent, year) %>% 
  slice(which.min(pop))
## Source: local data frame [15 x 6]
## Groups: continent, year
## 
##                  country continent year lifeExp     pop  gdpPercap
## 1  Sao Tome and Principe    Africa 1952  46.471   60011   879.5836
## 2  Sao Tome and Principe    Africa 1972  56.480   76595  1532.9853
## 3  Sao Tome and Principe    Africa 2002  64.337  170372  1353.0924
## 4    Trinidad and Tobago  Americas 1952  59.100  662850  3023.2719
## 5    Trinidad and Tobago  Americas 1972  65.900  975199  6619.5514
## 6    Trinidad and Tobago  Americas 2002  68.976 1101832 11460.6002
## 7                Bahrain      Asia 1952  50.939  120447  9867.0848
## 8                Bahrain      Asia 1972  63.300  230800 18268.6584
## 9                Bahrain      Asia 2002  74.795  656397 23403.5593
## 10               Iceland    Europe 1952  72.490  147962  7267.6884
## 11               Iceland    Europe 1972  74.460  209275 15798.0636
## 12               Iceland    Europe 2002  80.500  288030 31163.2020
## 13           New Zealand   Oceania 1952  69.390 1994794 10556.5757
## 14           New Zealand   Oceania 1972  71.890 2929100 16046.0373
## 15           New Zealand   Oceania 2002  79.110 3908037 23189.8014