There are different ways to rename characters and factors in the tidyverse and all of them use different syntax which makes it difficult for a part time user like me to remember the correct one.
Consider the simple reprex below.
dplyr::recode
uses a sequence of named replacements, with pattern first (name) and replacement second (value)
stringr::str_replace_all
uses a vector of named replacements, with pattern first (name) and replacement second (value)
forcats::fct_recode
uses a sequence of named replacements, with replacement first (name) and pattern second (value)
library(tidyverse)
library(lubridate)
library(forcats)
library(stringr)
df <- tribble( ~ alpha, ~ beta,
"green", 1,
"blue", 2,
"yellow", 3)
df %>%
mutate(alpha = dplyr::recode(alpha,
green = "black",
blue = "white"))
#> # A tibble: 3 x 2
#> alpha beta
#> <chr> <dbl>
#> 1 black 1
#> 2 white 2
#> 3 yellow 3
df %>%
mutate(alpha = stringr::str_replace_all(alpha,
c(green = "black",
blue = "white")))
#> # A tibble: 3 x 2
#> alpha beta
#> <chr> <dbl>
#> 1 black 1
#> 2 white 2
#> 3 yellow 3
df %>%
mutate(alpha = as_factor(alpha)) %>%
mutate(alpha = forcats::fct_recode(alpha,
black = "green",
white = "blue"))
#> # A tibble: 3 x 2
#> alpha beta
#> <fctr> <dbl>
#> 1 black 1
#> 2 white 2
#> 3 yellow 3
I wish it was always as simple as with renaming columns in dplyr, no quotes, replacement first:
library(tidyverse)
df <- tribble( ~ alpha, ~ beta,
"green", 1,
"blue", 2,
"yellow", 3)
df %>%
dplyr::select(delta = alpha, omega = beta)
#> # A tibble: 3 x 2
#> delta omega
#> <chr> <dbl>
#> 1 green 1
#> 2 blue 2
#> 3 yellow 3
df %>%
dplyr::rename(delta = alpha, omega = beta)
#> # A tibble: 3 x 2
#> delta omega
#> <chr> <dbl>
#> 1 green 1
#> 2 blue 2
#> 3 yellow 3
Any chance this can be harmonised across the tidyverse?