dplyr::mutate()
get_help()
docs
The mutate()
function is part of the {dplyr}
package, which is part of the {tidyverse}
.
We use this function to create new columns in tibbles (data frames). Since mutate()
will also overwrite existing columns, you can also use it to modify existing columns in place.
To use this function, you need to either first load the {dplyr}
library, or always use the function with dplyr::mutate()
notation.
# Load the library
library(dplyr)
# Or, load the full tidyverse:
library(tidyverse)
# Or, use :: notation
::mutate() dplyr
%>%
tibble mutate(name_of_new_column = what to put in new column)
%>%
tibble mutate(name_of_first_new_column = what to put in this new column,
name_of_second_new_column = what to put in this new column,
etc. as needed)
The examples below use the carnivores
dataset. Learn more about this dataset with get_help("carnivores")
.
# Show the carnivores dataset
carnivores
## # A tibble: 9 × 4
## name genus awake brainwt
## <chr> <fct> <dbl> <dbl>
## 1 Arctic fox Vulpes 11.5 0.0445
## 2 Cheetah Acinonyx 11.9 NA
## 3 Dog Canis 13.9 0.07
## 4 Gray seal Haliochoerus 17.8 0.325
## 5 Jaguar Panthera 13.6 0.157
## 6 Lion Panthera 10.5 NA
## 7 Northern fur seal Callorhinus 15.3 NA
## 8 Red fox Vulpes 14.2 0.0504
## 9 Tiger Panthera 8.2 NA
# Create a new column called `my_new_column` that literally contains the value `5` at all rows
%>%
carnivores mutate(my_new_column = 5)
## # A tibble: 9 × 5
## name genus awake brainwt my_new_column
## <chr> <fct> <dbl> <dbl> <dbl>
## 1 Arctic fox Vulpes 11.5 0.0445 5
## 2 Cheetah Acinonyx 11.9 NA 5
## 3 Dog Canis 13.9 0.07 5
## 4 Gray seal Haliochoerus 17.8 0.325 5
## 5 Jaguar Panthera 13.6 0.157 5
## 6 Lion Panthera 10.5 NA 5
## 7 Northern fur seal Callorhinus 15.3 NA 5
## 8 Red fox Vulpes 14.2 0.0504 5
## 9 Tiger Panthera 8.2 NA 5
# Create a new column `brainwt_g` that contains brain weight in g, instead of kg.
%>%
carnivores mutate(brainwt_g = brainwt * 1000) # multiply by 1000 to convert kg --> g
## # A tibble: 9 × 5
## name genus awake brainwt brainwt_g
## <chr> <fct> <dbl> <dbl> <dbl>
## 1 Arctic fox Vulpes 11.5 0.0445 44.5
## 2 Cheetah Acinonyx 11.9 NA NA
## 3 Dog Canis 13.9 0.07 70
## 4 Gray seal Haliochoerus 17.8 0.325 325
## 5 Jaguar Panthera 13.6 0.157 157
## 6 Lion Panthera 10.5 NA NA
## 7 Northern fur seal Callorhinus 15.3 NA NA
## 8 Red fox Vulpes 14.2 0.0504 50.4
## 9 Tiger Panthera 8.2 NA NA
# It is possible to create multiple columns at once:
%>%
carnivores mutate(brainwt_g = brainwt * 1000, # multiply by 1000 to convert kg --> g
brainwt_mg = brainwt * 1e6) # multiply by 1000000 (1e6) to convert kg --> mg
## # A tibble: 9 × 6
## name genus awake brainwt brainwt_g brainwt_mg
## <chr> <fct> <dbl> <dbl> <dbl> <dbl>
## 1 Arctic fox Vulpes 11.5 0.0445 44.5 44500
## 2 Cheetah Acinonyx 11.9 NA NA NA
## 3 Dog Canis 13.9 0.07 70 70000
## 4 Gray seal Haliochoerus 17.8 0.325 325 325000
## 5 Jaguar Panthera 13.6 0.157 157 157000
## 6 Lion Panthera 10.5 NA NA NA
## 7 Northern fur seal Callorhinus 15.3 NA NA NA
## 8 Red fox Vulpes 14.2 0.0504 50.4 50400
## 9 Tiger Panthera 8.2 NA NA NA
# It is also possible to use columns you're actively making when making new columns
%>%
carnivores mutate(brainwt_g = brainwt * 1000, # multiply by 1000 to convert kg --> g
brainwt_mg = brainwt_g * 1000) # you can use brainwt_g in code here
## # A tibble: 9 × 6
## name genus awake brainwt brainwt_g brainwt_mg
## <chr> <fct> <dbl> <dbl> <dbl> <dbl>
## 1 Arctic fox Vulpes 11.5 0.0445 44.5 44500
## 2 Cheetah Acinonyx 11.9 NA NA NA
## 3 Dog Canis 13.9 0.07 70 70000
## 4 Gray seal Haliochoerus 17.8 0.325 325 325000
## 5 Jaguar Panthera 13.6 0.157 157 157000
## 6 Lion Panthera 10.5 NA NA NA
## 7 Northern fur seal Callorhinus 15.3 NA NA NA
## 8 Red fox Vulpes 14.2 0.0504 50.4 50400
## 9 Tiger Panthera 8.2 NA NA NA
# Create a new column `little_name` that contains the `name` values in lower case.
# We'll use the function stringr::str_to_lower() to convert strings to lower case
%>%
carnivores mutate(little_name = stringr::str_to_lower(name))
## # A tibble: 9 × 5
## name genus awake brainwt little_name
## <chr> <fct> <dbl> <dbl> <chr>
## 1 Arctic fox Vulpes 11.5 0.0445 arctic fox
## 2 Cheetah Acinonyx 11.9 NA cheetah
## 3 Dog Canis 13.9 0.07 dog
## 4 Gray seal Haliochoerus 17.8 0.325 gray seal
## 5 Jaguar Panthera 13.6 0.157 jaguar
## 6 Lion Panthera 10.5 NA lion
## 7 Northern fur seal Callorhinus 15.3 NA northern fur seal
## 8 Red fox Vulpes 14.2 0.0504 red fox
## 9 Tiger Panthera 8.2 NA tiger
# Use mutate() to change the column `genus` into a factor instead of character
%>%
carnivores mutate(genus = as.factor(genus))
## # A tibble: 9 × 4
## name genus awake brainwt
## <chr> <fct> <dbl> <dbl>
## 1 Arctic fox Vulpes 11.5 0.0445
## 2 Cheetah Acinonyx 11.9 NA
## 3 Dog Canis 13.9 0.07
## 4 Gray seal Haliochoerus 17.8 0.325
## 5 Jaguar Panthera 13.6 0.157
## 6 Lion Panthera 10.5 NA
## 7 Northern fur seal Callorhinus 15.3 NA
## 8 Red fox Vulpes 14.2 0.0504
## 9 Tiger Panthera 8.2 NA