Notice how you can concatenate new columns in one mutate action and that the second uses the result of the first within the same command. But I am not sure whether this is always granted.
# A tibble: 142 × 7
country continent lifeExp pop gdpPercap richest perc_richest
<fct> <fct> <dbl> <int> <dbl> <dbl> <dbl>
1 Afghanistan Asia 42.1 25268405 727. 36023. 0.02
2 Albania Europe 75.7 3508512 4604. 44684. 0.1
3 Algeria Africa 71.0 31287142 5288. 12522. 0.42
4 Angola Africa 41.0 10866106 2773. 12522. 0.22
5 Argentina Americas 74.3 38331121 8798. 39097. 0.23
6 Australia Oceania 80.4 19546792 30688. 30688. 1
7 Austria Europe 79.0 8148312 32418. 44684. 0.73
8 Bahrain Asia 74.8 656397 23404. 36023. 0.65
9 Bangladesh Asia 62.0 135656790 1136. 36023. 0.03
10 Belgium Europe 78.3 10311970 30486. 44684. 0.68
# ℹ 132 more rows
Here we perform the same operation as with Americas, but this time for each continent. The richest column has identical values for countries on one continent.
Note the difference in the behavior of max() and division (/). The max function always looks at the entire group whereas division operates on each row independently.
# A tibble: 142 × 6
country continent lifeExp pop gdpPercap is_Asia
<fct> <fct> <dbl> <int> <dbl> <chr>
1 Afghanistan Asia 42.1 25268405 727. Asian country
2 Albania Europe 75.7 3508512 4604. Not in Asia
3 Algeria Africa 71.0 31287142 5288. Not in Asia
4 Angola Africa 41.0 10866106 2773. Not in Asia
5 Argentina Americas 74.3 38331121 8798. Not in Asia
6 Australia Oceania 80.4 19546792 30688. Not in Asia
7 Austria Europe 79.0 8148312 32418. Not in Asia
8 Bahrain Asia 74.8 656397 23404. Asian country
9 Bangladesh Asia 62.0 135656790 1136. Asian country
10 Belgium Europe 78.3 10311970 30486. Not in Asia
# ℹ 132 more rows
# A tibble: 142 × 7
country continent year lifeExp pop gdpPercap is_Asia
<fct> <fct> <int> <dbl> <int> <dbl> <chr>
1 Afghanistan Asia 2002 42.1 25268405 727. Asian country
2 Albania Europe 2002 75.7 3508512 4604. European country
3 Algeria Africa 2002 71.0 31287142 5288. African country
4 Angola Africa 2002 41.0 10866106 2773. African country
5 Argentina Americas 2002 74.3 38331121 8798. American country
6 Australia Oceania 2002 80.4 19546792 30688. Oceanian country
7 Austria Europe 2002 79.0 8148312 32418. European country
8 Bahrain Asia 2002 74.8 656397 23404. Asian country
9 Bangladesh Asia 2002 62.0 135656790 1136. Asian country
10 Belgium Europe 2002 78.3 10311970 30486. European country
# ℹ 132 more rows
Note the specific syntax of case_when!
8mutate existing with across
convert pop to millions and round to 3 decimal points
# A tibble: 142 × 5
country continent lifeExp pop gdpPercap
<fct> <fct> <dbl> <dbl> <dbl>
1 Afghanistan Asia 42.1 25.3 727.
2 Albania Europe 75.7 3.51 4604.
3 Algeria Africa 71.0 31.3 5288.
4 Angola Africa 41.0 10.9 2773.
5 Argentina Americas 74.3 38.3 8798.
6 Australia Oceania 80.4 19.5 30688.
7 Austria Europe 79.0 8.15 32418.
8 Bahrain Asia 74.8 0.656 23404.
9 Bangladesh Asia 62.0 136. 1136.
10 Belgium Europe 78.3 10.3 30486.
# ℹ 132 more rows
across lists the columns on which you want to perform the same transformation. Note its peculiar syntax: it is inside mutate and incorporates both the enumeration of the columns to transform and the transformation, which is written in the formula notation with tilde and .x for the first argument.