library(tidyverse)
library(gapminder)HDS 5.4.2
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(gdpPercap >= 5000 & gdpPercap <= 1000000)# A tibble: 692 × 6
country continent year lifeExp pop gdpPercap
<fct> <fct> <int> <dbl> <int> <dbl>
1 Albania Europe 2007 76.4 3600523 5937.
2 Algeria Africa 1982 61.4 20033753 5745.
3 Algeria Africa 1987 65.8 23254956 5681.
4 Algeria Africa 1992 67.7 26298373 5023.
5 Algeria Africa 2002 71.0 31287142 5288.
6 Algeria Africa 2007 72.3 33333216 6223.
7 Angola Africa 1967 36.0 5247469 5523.
8 Angola Africa 1972 37.9 5894858 5473.
9 Argentina Americas 1952 62.5 17876956 5911.
10 Argentina Americas 1957 64.4 19610538 6857.
# ℹ 682 more rows