magrittr::%<>%
get_help() docs
The combined symbol %<>% is an operator in the {magrittr} package, which is part of the {tidyverse}. This operator simultaneously pipes and redefines the first input as the result. It is useful for simultaneously wrangling and modifying tibbles (data frames) in place.
To learn more about magrittr piping, run get_help("%>%").
To use the assignment pipe, you need to either first load the {tidyverse} or the {magrittr} library.
# Load the tidyverse (most common)
library(tidyverse)
# Or load magrittr
library(magrittr)tibble %<>%
mutate(new_column = contents of new column)
# The above is equivalent to:
tibble %>%
mutate(new_column = contents of new column) -> tibbleThe example below uses 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# Simultaneously rename the column `name` to `species` and redefine carnivores
carnivores %<>%
rename(species = name)
carnivores
## # A tibble: 9 × 4
## species 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