tidyverse / magrittr

Improve the readability of R code with the pipe
https://magrittr.tidyverse.org
Other
957 stars 157 forks source link

add alias for Encoding #209

Closed cstepper closed 4 years ago

cstepper commented 4 years ago

Hi, just added an Alias for Encoding<-.

Makes it more concise for me working across multiple columns.

set_encoding <- `Encoding<-`

  tab = tab %>%
    mutate(across(c(col_a, col_b),
                  set_encoding, "UTF-8"))

compared to

   tab = tab %>% 
     mutate(across(c(col_a, col_b),
                  function(x) {
                    Encoding(x) <- "UTF-8"
                    return(x)
                  })
           )

Thanks, Christoph

hadley commented 4 years ago

You can just do

tab <- tab %>%
    mutate(across(c(col_a, col_b), `Encoding<-`, "UTF-8"))
cstepper commented 4 years ago

Thanks and yes, this is an elegant solution. Hope I can remember next time I need this ;)