Use fixed() for multiple patterns in stringr::str_replace_all()
I'd like to str_replace()
multiple fixed()
patterns at once, but the
c("pattern1" = "replacement1", "pattern2" = "replacement2", ...)
syntax is not working with
c(fixed("pattern1") = "replacement1", fixed("pattern2") = "replacement2", ...)
It looks like this name = value
syntax is not possible, when I need to have a function call on the LHS.
Any ideas on how to code this with stringr::str_replace_all()
?
str_replace_all()
works fine when just using the regex default. I could also manage the underlying stringi::stri_replace_all_fixed()
function to work.
library(dplyr)
library(stringr)
sw <- dplyr::starwars %>%
mutate(
hair_skin_eye = str_c(hair_color, skin_color, eye_color, sep = " | ")
) %>%
select(name, hair_skin_eye)
# using default (regex)
sw %>%
mutate(
hair_skin_eye_repl = str_replace_all(
string = hair_skin_eye,
c(
"white" = "WHITE",
"blue" = "BLUE",
"yellow" = "YELLOW"
)
)
)
#> # A tibble: 87 x 3
#> name hair_skin_eye hair_skin_eye_repl
#> <chr> <chr> <chr>
#> 1 Luke Skywalker blond | fair | blue blond | fair | BLUE
#> 2 C-3PO <NA> <NA>
#> 3 R2-D2 <NA> <NA>
#> 4 Darth Vader none | white | yellow none | WHITE | YELLOW
#> 5 Leia Organa brown | light | brown brown | light | brown
#> 6 Owen Lars brown, grey | light | blue brown, grey | light | BLUE
#> 7 Beru Whitesun la~ brown | light | blue brown | light | BLUE
#> 8 R5-D4 <NA> <NA>
#> 9 Biggs Darklighter black | light | brown black | light | brown
#> 10 Obi-Wan Kenobi auburn, white | fair | blue-~ auburn, WHITE | fair | BLUE-~
#> # ... with 77 more rows
# using just one pattern (fixed)
sw %>%
mutate(
hair_skin_eye_repl = str_replace_all(
string = hair_skin_eye,
pattern = fixed("white"),
replacement = "WHITE")
)
#> # A tibble: 87 x 3
#> name hair_skin_eye hair_skin_eye_repl
#> <chr> <chr> <chr>
#> 1 Luke Skywalker blond | fair | blue blond | fair | blue
#> 2 C-3PO <NA> <NA>
#> 3 R2-D2 <NA> <NA>
#> 4 Darth Vader none | white | yellow none | WHITE | yellow
#> 5 Leia Organa brown | light | brown brown | light | brown
#> 6 Owen Lars brown, grey | light | blue brown, grey | light | blue
#> 7 Beru Whitesun la~ brown | light | blue brown | light | blue
#> 8 R5-D4 <NA> <NA>
#> 9 Biggs Darklighter black | light | brown black | light | brown
#> 10 Obi-Wan Kenobi auburn, white | fair | blue-~ auburn, WHITE | fair | blue-~
#> # ... with 77 more rows
# use underlying stringi function directly
sw %>%
mutate(
hair_skin_eye_repl = stringi::stri_replace_all_fixed(
str = hair_skin_eye,
pattern = c("white", "blue", "yellow"),
replacement = c("WHITE", "BLUE", "YELLOW"),
vectorize_all = FALSE
)
)
#> # A tibble: 87 x 3
#> name hair_skin_eye hair_skin_eye_repl
#> <chr> <chr> <chr>
#> 1 Luke Skywalker blond | fair | blue blond | fair | BLUE
#> 2 C-3PO <NA> <NA>
#> 3 R2-D2 <NA> <NA>
#> 4 Darth Vader none | white | yellow none | WHITE | YELLOW
#> 5 Leia Organa brown | light | brown brown | light | brown
#> 6 Owen Lars brown, grey | light | blue brown, grey | light | BLUE
#> 7 Beru Whitesun la~ brown | light | blue brown | light | BLUE
#> 8 R5-D4 <NA> <NA>
#> 9 Biggs Darklighter black | light | brown black | light | brown
#> 10 Obi-Wan Kenobi auburn, white | fair | blue-~ auburn, WHITE | fair | BLUE-~
#> # ... with 77 more rows
# using multiple patterns - doesn't work
sw %>%
mutate(
hair_skin_eye_repl = stringr::str_replace_all(
string = hair_skin_eye,
c(
fixed("white") = "WHITE",
fixed("blue") = "BLUE",
fixed("yellow") = "YELLOW"
)
)
)
#> Error: unexpected '=' in:
#> " c(
#> fixed("white") ="
You can pass a named vector in str_replace_all
:
library(dplyr)
library(stringr)
sw %>%
mutate(
hair_skin_eye_repl = stringr::str_replace_all(
string = hair_skin_eye,
setNames(c('WHITE', 'BLUE', 'YELLOW'), c('white', 'blue', 'yellow'))
)
)
# name hair_skin_eye hair_skin_eye_repl
# <chr> <chr> <chr>
# 1 Luke Skywalker blond | fair | blue blond | fair | BLUE
# 2 C-3PO NA NA
# 3 R2-D2 NA NA
# 4 Darth Vader none | white | yellow none | WHITE | YELLOW
# 5 Leia Organa brown | light | brown brown | light | brown
# 6 Owen Lars brown, grey | light | blue brown, grey | light | BLUE
# 7 Beru Whitesun lars brown | light | blue brown | light | BLUE
# 8 R5-D4 NA NA
# 9 Biggs Darklighter black | light | brown black | light | brown
#10 Obi-Wan Kenobi auburn, white | fair | blue-gray auburn, WHITE | fair | BLUE-gray
# … with 77 more rows