mutate_if- your friend. If you don't like converting to a character, you can simply use
raw %>% mutate_if(is.factor, trimws)
which suggests that you can simply convert to a coefficient:
raw %>% mutate_if(is.factor, funs(factor(trimws(.))))
If you want to keep the type you can use more confusing
raw %>% mutate_if(is.factor, funs(`levels<-`(., trimws(levels(.)))))
The base equivalent of R will be
raw[] <- lapply(raw, function(x){if (is.factor(x)) {levels(x) <- trimws(levels(x))} ; x})
, , , :
levels(raw$a) <- trimws(levels(raw$a))
: forcats::relabel ( tidyverse) :
raw %>% mutate_if(is.factor, fct_relabel, trimws)
,
raw %>% mutate(a = fct_relabel(a, trimws))
, purrr-style ~trimws(.x), .