You can do something like this
library(dplyr)
dataInExample %>%
mutate(across(.cols = c(b, g),
.fns = as.factor))
If you need more specific help, please provide a proper REPRoducible EXample (reprex) illustrating your issue.