The function dplyr::mutate()
allows you to:
- Create new columns based on existing columns.
- Modify existing columns
- Delete columns
Tidyverse reference page
Dataset
For demonstration we’ll load the mammal_sleep_tbl
data from the mgrtibbles package (hyperlink includes install instructions).
#Load package
library("mgrtibbles")
#mammal_sleep_tbl tibble for demonstration
mgrtibbles::mammal_sleep_tbl
# A tibble: 62 × 11
species body_wt brain_wt non_dreaming dreaming total_sleep life_span
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Africanelephant 6.65e+3 5.71 NA NA 3.3 38.6
2 Africangiantpou… 1 e+0 0.0066 6.3 2 8.3 4.5
3 ArcticFox 3.38e+0 0.0445 NA NA 12.5 14
4 Arcticgroundsqu… 9.2 e-1 0.0057 NA NA 16.5 NA
5 Asianelephant 2.55e+3 4.60 2.1 1.8 3.9 69
6 Baboon 1.06e+1 0.180 9.1 0.7 9.8 27
7 Bigbrownbat 2.3 e-2 0.0003 15.8 3.9 19.7 19
8 Braziliantapir 1.6 e+2 0.169 5.2 1 6.2 30.4
9 Cat 3.3 e+0 0.0256 10.9 3.6 14.5 28
10 Chimpanzee 5.22e+1 0.44 8.3 1.4 9.7 50
# ℹ 52 more rows
# ℹ 4 more variables: gestation <dbl>, predation <fct>, exposure <fct>,
# danger <fct>
Create new column
Create a column of brain and body weight ratio.
mammal_sleep_tbl |>
#Select columns of interest
dplyr::select(species, body_wt, brain_wt) |>
#Calculate and add brain_body_wt_ratio column
dplyr::mutate(brain_body_wt_ratio = brain_wt/(body_wt*1000))
# A tibble: 62 × 4
species body_wt brain_wt brain_body_wt_ratio
<chr> <dbl> <dbl> <dbl>
1 Africanelephant 6654 5.71 0.000000858
2 Africangiantpouchedrat 1 0.0066 0.0000066
3 ArcticFox 3.38 0.0445 0.0000131
4 Arcticgroundsquirrel 0.92 0.0057 0.00000620
5 Asianelephant 2547 4.60 0.00000181
6 Baboon 10.6 0.180 0.0000170
7 Bigbrownbat 0.023 0.0003 0.0000130
8 Braziliantapir 160 0.169 0.00000106
9 Cat 3.3 0.0256 0.00000776
10 Chimpanzee 52.2 0.44 0.00000844
# ℹ 52 more rows
Create a column for life time sleep
mammal_sleep_tbl |>
#Select columns of interest
dplyr::select(species, total_sleep, life_span) |>
#Calculate and add total_life_sleep column
dplyr::mutate(total_life_sleep = (life_span*365.25) * total_sleep)
# A tibble: 62 × 4
species total_sleep life_span total_life_sleep
<chr> <dbl> <dbl> <dbl>
1 Africanelephant 3.3 38.6 46526.
2 Africangiantpouchedrat 8.3 4.5 13642.
3 ArcticFox 12.5 14 63919.
4 Arcticgroundsquirrel 16.5 NA NA
5 Asianelephant 3.9 69 98289.
6 Baboon 9.8 27 96645.
7 Bigbrownbat 19.7 19 136713.
8 Braziliantapir 6.2 30.4 68842.
9 Cat 14.5 28 148292.
10 Chimpanzee 9.7 50 177146.
# ℹ 52 more rows