Not that you need another solution, but it could be handy to combine all your steps into a single function to tidy up your mutate
call. You can combine a string of functions easily with purrr::compose
to run them in the given order each time you need them.
Using G. Grothendieck's excellent code split into anonymous functions:
library(tidyverse)
df <- data.frame(
c1 = c("Élève", "Café", "Château", "Noël", "Crème")
)
tidy_text <- compose(
\(t) gsub("\\s+", " ", t),
\(t) gsub("\"", "", t),
toupper,
\(t) iconv(t, to = "ASCII//TRANSLIT")
)
df %>%
mutate(c1 = tidy_text(c1))
#> c1
#> 1 ELEVE
#> 2 CAFE
#> 3 CHATEAU
#> 4 NOEL
#> 5 CREME
Or using Mark's tidyverse code and purrr
formula/function syntax:
tidy_text2 <- compose(
str_squish,
~ str_remove_all(.x, "\""),
str_to_upper,
~ iconv(.x, to = "ASCII//TRANSLIT")
)
df %>%
mutate(c1 = tidy_text2(c1))
#> c1
#> 1 ELEVE
#> 2 CAFE
#> 3 CHATEAU
#> 4 NOEL
#> 5 CREME
May not be necessary if you're only using it once of course! Just one way of having some bits tidier than others!