HDS 5.4.2

Author

Michael Ernst

library(tidyverse)
library(gapminder)

Begin by loading the tidyverse and gapminder packages in the code chunk above and adding your name as the author.

The gapminder data frame contained in the gapminder package gives data on life expectancy, GDP per capita, and population by country. We would like to subset the data frame by rows using the filter() function.

filtering the gapminder Data

Let’s start by finding all of the rows for countries in Europe. Modify this code by filling in the ______ to do so:

gapminder |>
  filter(continent == "Europe")
# A tibble: 360 × 6
   country continent  year lifeExp     pop gdpPercap
   <fct>   <fct>     <int>   <dbl>   <int>     <dbl>
 1 Albania Europe     1952    55.2 1282697     1601.
 2 Albania Europe     1957    59.3 1476505     1942.
 3 Albania Europe     1962    64.8 1728137     2313.
 4 Albania Europe     1967    66.2 1984060     2760.
 5 Albania Europe     1972    67.7 2263554     3313.
 6 Albania Europe     1977    68.9 2509048     3533.
 7 Albania Europe     1982    70.4 2780097     3631.
 8 Albania Europe     1987    72   3075321     3739.
 9 Albania Europe     1992    71.6 3326498     2497.
10 Albania Europe     1997    73.0 3428038     3193.
# ℹ 350 more rows

Find all the rows for countries in Asia and Oceania:

gapminder |>
  filter(continent == "Asia" | continent == "Oceania")
# A tibble: 420 × 6
   country     continent  year lifeExp      pop gdpPercap
   <fct>       <fct>     <int>   <dbl>    <int>     <dbl>
 1 Afghanistan Asia       1952    28.8  8425333      779.
 2 Afghanistan Asia       1957    30.3  9240934      821.
 3 Afghanistan Asia       1962    32.0 10267083      853.
 4 Afghanistan Asia       1967    34.0 11537966      836.
 5 Afghanistan Asia       1972    36.1 13079460      740.
 6 Afghanistan Asia       1977    38.4 14880372      786.
 7 Afghanistan Asia       1982    39.9 12881816      978.
 8 Afghanistan Asia       1987    40.8 13867957      852.
 9 Afghanistan Asia       1992    41.7 16317921      649.
10 Afghanistan Asia       1997    41.8 22227415      635.
# ℹ 410 more rows

Find all the rows where a country’s life expectancy is less than 40:

gapminder |>
  filter(lifeExp < 40)
# A tibble: 124 × 6
   country     continent  year lifeExp      pop gdpPercap
   <fct>       <fct>     <int>   <dbl>    <int>     <dbl>
 1 Afghanistan Asia       1952    28.8  8425333      779.
 2 Afghanistan Asia       1957    30.3  9240934      821.
 3 Afghanistan Asia       1962    32.0 10267083      853.
 4 Afghanistan Asia       1967    34.0 11537966      836.
 5 Afghanistan Asia       1972    36.1 13079460      740.
 6 Afghanistan Asia       1977    38.4 14880372      786.
 7 Afghanistan Asia       1982    39.9 12881816      978.
 8 Angola      Africa     1952    30.0  4232095     3521.
 9 Angola      Africa     1957    32.0  4561361     3828.
10 Angola      Africa     1962    34    4826015     4269.
# ℹ 114 more rows

Find all the rows where a country’s life expectancy is less than 40 in 2007:

gapminder |>
  filter(lifeExp < 40, year == 2007)
# A tibble: 1 × 6
  country   continent  year lifeExp     pop gdpPercap
  <fct>     <fct>     <int>   <dbl>   <int>     <dbl>
1 Swaziland Africa     2007    39.6 1133066     4513.

Find all the rows for countries other than France:

gapminder |>
  filter(country != "France")
# A tibble: 1,692 × 6
   country     continent  year lifeExp      pop gdpPercap
   <fct>       <fct>     <int>   <dbl>    <int>     <dbl>
 1 Afghanistan Asia       1952    28.8  8425333      779.
 2 Afghanistan Asia       1957    30.3  9240934      821.
 3 Afghanistan Asia       1962    32.0 10267083      853.
 4 Afghanistan Asia       1967    34.0 11537966      836.
 5 Afghanistan Asia       1972    36.1 13079460      740.
 6 Afghanistan Asia       1977    38.4 14880372      786.
 7 Afghanistan Asia       1982    39.9 12881816      978.
 8 Afghanistan Asia       1987    40.8 13867957      852.
 9 Afghanistan Asia       1992    41.7 16317921      649.
10 Afghanistan Asia       1997    41.8 22227415      635.
# ℹ 1,682 more rows

Find all the rows for the following countries:

  • Denmark
  • Finland
  • Iceland
  • Norway
  • Sweden
gapminder |>
  filter(country %in% c("Denmark", "Finland", "Iceland", "Norway", "Sweden"))
# A tibble: 60 × 6
   country continent  year lifeExp     pop gdpPercap
   <fct>   <fct>     <int>   <dbl>   <int>     <dbl>
 1 Denmark Europe     1952    70.8 4334000     9692.
 2 Denmark Europe     1957    71.8 4487831    11100.
 3 Denmark Europe     1962    72.4 4646899    13583.
 4 Denmark Europe     1967    73.0 4838800    15937.
 5 Denmark Europe     1972    73.5 4991596    18866.
 6 Denmark Europe     1977    74.7 5088419    20423.
 7 Denmark Europe     1982    74.6 5117810    21688.
 8 Denmark Europe     1987    74.8 5127024    25116.
 9 Denmark Europe     1992    75.3 5171393    26407.
10 Denmark Europe     1997    76.1 5283663    29804.
# ℹ 50 more rows

Find all the rows where a country’s GDP per capita is at least 10,000:

gapminder |>
  filter(gdpPercap >= 10000)
# A tibble: 392 × 6
   country   continent  year lifeExp      pop gdpPercap
   <fct>     <fct>     <int>   <dbl>    <int>     <dbl>
 1 Argentina Americas   1977    68.5 26983828    10079.
 2 Argentina Americas   1997    73.3 36203463    10967.
 3 Argentina Americas   2007    75.3 40301927    12779.
 4 Australia Oceania    1952    69.1  8691212    10040.
 5 Australia Oceania    1957    70.3  9712569    10950.
 6 Australia Oceania    1962    70.9 10794968    12217.
 7 Australia Oceania    1967    71.1 11872264    14526.
 8 Australia Oceania    1972    71.9 13177000    16789.
 9 Australia Oceania    1977    73.5 14074100    18334.
10 Australia Oceania    1982    74.7 15184200    19477.
# ℹ 382 more rows

Find all the rows for countries in Asia with GDP per capita of at least 5,000 and a population no more than 1,000,000:

gapminder |>
  filter(continent == "Asia", gdpPercap >= 5000, pop <= 1000000)
# A tibble: 18 × 6
   country continent  year lifeExp    pop gdpPercap
   <fct>   <fct>     <int>   <dbl>  <int>     <dbl>
 1 Bahrain Asia       1952    50.9 120447     9867.
 2 Bahrain Asia       1957    53.8 138655    11636.
 3 Bahrain Asia       1962    56.9 171863    12753.
 4 Bahrain Asia       1967    59.9 202182    14805.
 5 Bahrain Asia       1972    63.3 230800    18269.
 6 Bahrain Asia       1977    65.6 297410    19340.
 7 Bahrain Asia       1982    69.1 377967    19211.
 8 Bahrain Asia       1987    70.8 454612    18524.
 9 Bahrain Asia       1992    72.6 529491    19036.
10 Bahrain Asia       1997    73.9 598561    20292.
11 Bahrain Asia       2002    74.8 656397    23404.
12 Bahrain Asia       2007    75.6 708573    29796.
13 Kuwait  Asia       1952    55.6 160000   108382.
14 Kuwait  Asia       1957    58.0 212846   113523.
15 Kuwait  Asia       1962    60.5 358266    95458.
16 Kuwait  Asia       1967    64.6 575003    80895.
17 Kuwait  Asia       1972    67.7 841934   109348.
18 Oman    Asia       1972    52.1 829050    10618.