Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we avoid repeating the column name each time when using it in mutates

I use several mutate on the same column. How can we only use mutate once and without repeating the column name?

df <- data.frame(
  c1 = c("Élève", "Café", "Château", "Noël", "Crème")
)

df2 <- df %>% 
  mutate(c1 = trimws(c1)) %>%
  mutate(c1 = gsub("\\s+", " ", c1)) %>%
  mutate(c1 = gsub("\"", "", c1)) %>%
  mutate(c1 = iconv(toupper(c1), to = "ASCII//TRANSLIT"))
  
like image 310
dia05 Avatar asked Oct 20 '25 15:10

dia05


2 Answers

Place the pipeline within the mutate like this:

df3 <- df %>%
  mutate(c1 = c1 %>%
    trimws %>%
    gsub("\\s+", " ", .) %>%
    gsub("\"", "", .) %>%
    toupper %>%
    iconv(to = "ASCII//TRANSLIT"))

identical(df2, df3)
## [1] TRUE

or

df4 <- df %>%
  reframe(across(c1, . %>%
    trimws %>%
    gsub("\\s+", " ", .) %>%
    gsub("\"", "", .) %>%
    toupper %>%
    iconv(to = "ASCII//TRANSLIT")))

identical(df2, df4)
## [1] TRUE
like image 91
G. Grothendieck Avatar answered Oct 22 '25 03:10

G. Grothendieck


You can use pipes within mutate calls! Also, even if that weren't the case, columns you create in a mutate function call can be used later within the same function call. So you could keep on redefining c1 within one mutate call.

But anyway, this is how I would do it (using almost all stringr functions):

library(stringr)

df2 <- df |>
  mutate(c1 = str_squish(c1) |>
              str_remove_all("\"") |>
              str_to_upper() |>
              iconv(to = "ASCII//TRANSLIT"))
like image 28
Mark Avatar answered Oct 22 '25 03:10

Mark



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!