Quantitative Methods for
International Politics
IPOL 3270 • Fall 2026
September 13, 2026
filter()select()arrange()mutate()summarise()group_by()gapminder datagapminder data# A tibble: 1,704 × 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,694 more rows


Extract rows with filter() |
|
Extract columns with select() |
|
Arrange/sort rows with arrange() |
|
Make new columns with mutate() |
|
Make group summaries withgroup_by() |> summarise() |
filter()filter()Extract rows that meet some sort of test
| country | continent | year |
|---|---|---|
| Afghanistan | Asia | 1952 |
| Afghanistan | Asia | 1957 |
| Afghanistan | Asia | 1962 |
| Afghanistan | Asia | 1967 |
| Afghanistan | Asia | 1972 |
| … | … | … |
| country | continent | year |
|---|---|---|
| Egypt | Africa | 1952 |
| Egypt | Africa | 1957 |
| Egypt | Africa | 1962 |
| Egypt | Africa | 1967 |
| Egypt | Africa | 1972 |
| Egypt | Africa | 1977 |
What’s the difference here?
<-<- assigns the output from the righthand side to an object with the name on the lefthand side

filter()| Test | Meaning | Test | Meaning |
|---|---|---|---|
x < y |
Less than | x %in% y |
In (group membership) |
x > y |
Greater than | is.na(x) |
Is missing |
x == y |
Equal to | !is.na(x) |
Is not missing |
x <= y |
Less than or equal to | ||
x >= y |
Greater than or equal to | ||
x != y |
Not equal to |
Use filter() and logical tests to show…
filter() with multiple conditionsExtract rows that meet every test
filter() with multiple conditions| country | continent | year |
|---|---|---|
| Afghanistan | Asia | 1952 |
| Afghanistan | Asia | 1957 |
| Afghanistan | Asia | 1962 |
| Afghanistan | Asia | 1967 |
| Afghanistan | Asia | 1972 |
| … | … | … |
| country | continent | year |
|---|---|---|
| Egypt | Africa | 2002 |
| Egypt | Africa | 2007 |
| Operator | Meaning |
|---|---|
a & b |
and |
a | b |
or |
!a |
not |
These do the same thing:
Use filter() and Boolean logical tests to show…
Collapsing multiple tests into one
Using multiple tests instead of %in%
Every {dplyr} verb function follows the same pattern!
select()select()Keep specific columns
| country | continent | year |
|---|---|---|
| Afghanistan | Asia | 1952 |
| Afghanistan | Asia | 1957 |
| Afghanistan | Asia | 1962 |
| Afghanistan | Asia | 1967 |
| Afghanistan | Asia | 1972 |
| … | … | … |
| year | country |
|---|---|
| 1952 | Afghanistan |
| 1957 | Afghanistan |
| 1962 | Afghanistan |
| 1967 | Afghanistan |
| 1972 | Afghanistan |
| … | … |
-| country | year | lifeExp | pop | gdpPercap |
|---|---|---|---|---|
| Afghanistan | 1952 | 28.801 | 8425333 | 779.4453145 |
| Afghanistan | 1957 | 30.332 | 9240934 | 820.8530296 |
| Afghanistan | 1962 | 31.997 | 10267083 | 853.10071 |
| Afghanistan | 1967 | 34.02 | 11537966 | 836.1971382 |
| Afghanistan | 1972 | 36.088 | 13079460 | 739.9811058 |
| … | … | … | … | … |
everything()| year | pop | country | continent | lifeExp | gdpPercap |
|---|---|---|---|---|---|
| 1952 | 8425333 | Afghanistan | Asia | 28.801 | 779.4453145 |
| 1957 | 9240934 | Afghanistan | Asia | 30.332 | 820.8530296 |
| 1962 | 10267083 | Afghanistan | Asia | 31.997 | 853.10071 |
| 1967 | 11537966 | Afghanistan | Asia | 34.02 | 836.1971382 |
| 1972 | 13079460 | Afghanistan | Asia | 36.088 | 739.9811058 |
| … | … | … | … | … | … |
| country | continent |
|---|---|
| Afghanistan | Asia |
| Afghanistan | Asia |
| Afghanistan | Asia |
| Afghanistan | Asia |
| Afghanistan | Asia |
| … | … |
| Operator | Meaning |
|---|---|
starts_with() |
Starts with an exact prefix |
ends_with() |
Ends with an exact suffix |
contains() |
Contains some text |
matches() |
Matches a search |
num_range() |
Contains a range of numbers |
Use select() to…
gdpPercap columnyear be the first column and keep all the othersarrange()arrange()Sort rows by specific columns
| country | year | lifeExp |
|---|---|---|
| Afghanistan | 1952 | 28.801 |
| Afghanistan | 1957 | 30.332 |
| Afghanistan | 1962 | 31.997 |
| Afghanistan | 1967 | 34.02 |
| Afghanistan | 1972 | 36.088 |
| … | … | … |
| country | year | lifeExp |
|---|---|---|
| Rwanda | 1992 | 23.599 |
| Afghanistan | 1952 | 28.801 |
| Gambia | 1952 | 30 |
| Angola | 1952 | 30.015 |
| Sierra Leone | 1952 | 30.331 |
| … | … | … |
desc()| country | year | lifeExp |
|---|---|---|
| Afghanistan | 1952 | 28.801 |
| Afghanistan | 1957 | 30.332 |
| Afghanistan | 1962 | 31.997 |
| Afghanistan | 1967 | 34.02 |
| Afghanistan | 1972 | 36.088 |
| … | … | … |
| country | year | lifeExp |
|---|---|---|
| Japan | 2007 | 82.603 |
| Hong Kong, China | 2007 | 82.208 |
| Japan | 2002 | 82 |
| Iceland | 2007 | 81.757 |
| Switzerland | 2007 | 81.701 |
| … | … | … |
| country | year | lifeExp |
|---|---|---|
| Afghanistan | 1952 | 28.801 |
| Afghanistan | 1957 | 30.332 |
| Afghanistan | 1962 | 31.997 |
| Afghanistan | 1967 | 34.02 |
| Afghanistan | 1972 | 36.088 |
| … | … | … |
| country | year | lifeExp |
|---|---|---|
| Swaziland | 2007 | 39.613 |
| Mozambique | 2007 | 42.082 |
| Zambia | 2007 | 42.384 |
| Sierra Leone | 2007 | 42.568 |
| Lesotho | 2007 | 42.592 |
| … | … | … |
Use arrange() to…
You can’t! It’ll come with practice.
mutate()mutate()Create new columns
| country | year | gdpPercap | pop |
|---|---|---|---|
| Afghanistan | 1952 | 779 | 8,425,333 |
| Afghanistan | 1957 | 821 | 9,240,934 |
| Afghanistan | 1962 | 853 | 10,267,083 |
| Afghanistan | 1967 | 836 | 11,537,966 |
| Afghanistan | 1972 | 740 | 13,079,460 |
| … | … | … | … |
| country | year | … | gdp |
|---|---|---|---|
| Afghanistan | 1952 | … | 6,567,086,330 |
| Afghanistan | 1957 | … | 7,585,448,670 |
| Afghanistan | 1962 | … | 8,758,855,797 |
| Afghanistan | 1967 | … | 9,648,014,150 |
| Afghanistan | 1972 | … | 9,678,553,274 |
| Afghanistan | 1977 | … | 11,697,659,231 |
| country | year | gdpPercap | pop |
|---|---|---|---|
| Afghanistan | 1952 | 779 | 8,425,333 |
| Afghanistan | 1957 | 821 | 9,240,934 |
| Afghanistan | 1962 | 853 | 10,267,083 |
| Afghanistan | 1967 | 836 | 11,537,966 |
| Afghanistan | 1972 | 740 | 13,079,460 |
| … | … | … | … |
| country | year | … | gdp | pop_mil |
|---|---|---|---|---|
| Afghanistan | 1952 | … | 6,567,086,330 | 8 |
| Afghanistan | 1957 | … | 7,585,448,670 | 9 |
| Afghanistan | 1962 | … | 8,758,855,797 | 10 |
| Afghanistan | 1967 | … | 9,648,014,150 | 12 |
| Afghanistan | 1972 | … | 9,678,553,274 | 13 |
| Afghanistan | 1977 | … | 11,697,659,231 | 15 |
ifelse()Do conditional tests within mutate()
Use mutate() to…
africa column that is TRUE if the country is on the African continentlog())africa_asia column that says “Africa or Asia” if the country is in Africa or Asia, and “Not Africa or Asia” if it’s notrename()Rename existing columns
…EXCEPT (1) they can’t start with numbers and (2) they can’t have spaces
Make a dataset for just 2002 and calculate logged GDP per capita
Make a dataset for just 2002 and calculate logged GDP per capita
Make a dataset for just 2002 and calculate logged GDP per capita
The |> operator (pipe) takes an object on the left and passes it as the first argument of the function on the right
These do the same thing!
Make a dataset for just 2002 and calculate logged GDP per capita
|>|> vs %>%There are actually multiple pipes
%>% was invented first, but requires a package to use
|> is part of base R
They’re interchangeable 99% of the time
(Just be consistent)
summarise()summarise()Compute a table of summaries
| country | continent | year | lifeExp |
|---|---|---|---|
| Afghanistan | Asia | 1952 | 28.801 |
| Afghanistan | Asia | 1957 | 30.332 |
| Afghanistan | Asia | 1962 | 31.997 |
| Afghanistan | Asia | 1967 | 34.02 |
| … | … | … | … |
| mean_life |
|---|
| 59.47444 |
summarise()| country | continent | year | lifeExp |
|---|---|---|---|
| Afghanistan | Asia | 1952 | 28.801 |
| Afghanistan | Asia | 1957 | 30.332 |
| Afghanistan | Asia | 1962 | 31.997 |
| Afghanistan | Asia | 1967 | 34.02 |
| Afghanistan | Asia | 1972 | 36.088 |
| … | … | … | … |
| mean_life | min_life |
|---|---|
| 59.47444 | 23.599 |
Use summarise() to calculate…
| first | last | num_rows | num_unique |
|---|---|---|---|
| 1952 | 2007 | 1704 | 142 |
Use filter() and summarise() to calculate (1) the number of unique countries and (2) the median life expectancy in Africa in 2007
| n_countries | med_le |
|---|---|
| 52 | 52.9265 |
group_by()group_by()Put rows into groups based on values in a column
Nothing happens by itself!
Powerful when combined with summarise()
group_by() |> summarise()| continent | n_countries | avg_life_exp |
|---|---|---|
| Africa | 52 | 48.86533 |
| Americas | 25 | 64.65874 |
| Asia | 33 | 60.06490 |
| Europe | 30 | 71.90369 |
| Oceania | 2 | 74.32621 |
| city | particle_size | amount |
|---|---|---|
| New York | Large | 23 |
| New York | Small | 14 |
| London | Large | 22 |
| London | Small | 16 |
| Beijing | Large | 121 |
| Beijing | Small | 56 |
| mean | sum | n |
|---|---|---|
| 42 | 252 | 6 |
| city | particle_size | amount |
|---|---|---|
| New York | Large | 23 |
| New York | Small | 14 |
| London | Large | 22 |
| London | Small | 16 |
| Beijing | Large | 121 |
| Beijing | Small | 56 |
| city | mean | sum | n |
|---|---|---|---|
| Beijing | 88.5 | 177 | 2 |
| London | 19.0 | 38 | 2 |
| New York | 18.5 | 37 | 2 |
| city | particle_size | amount |
|---|---|---|
| New York | Large | 23 |
| New York | Small | 14 |
| London | Large | 22 |
| London | Small | 16 |
| Beijing | Large | 121 |
| Beijing | Small | 56 |
| particle_size | mean | sum | n |
|---|---|---|---|
| Large | 55.33333 | 166 | 3 |
| Small | 28.66667 | 86 | 3 |
Find the minimum, maximum, and median life expectancy for each continent
Find the minimum, maximum, and median life expectancy for each continent in 2007 only