I would like to relabel the levels of factors as follows:
i. If the level is of length 3 and above relabel it to sentence case otherwise do title case
ie home doing nothing becomes Home doing nothing, yes becomes Yes and good practice becomes Good Practice
Is there any way to do this?
library(tidyverse)
vars <- c("a", "b", "c", "d", "e")
mydata <- tribble(
~"a", ~"b", ~"c", ~"d", ~"e", ~"id",
"yes", "school in Kenya", "r", 10, "good practice", 1,
"no", "home doing nothing", "python", 12, "doing well", 3,
"no", "school in Tanzania", "c++", 35, "by walking", 4,
"yes", "home practising", "c", 65, "practising everyday", 5,
"no", "home", "java", 78, "sitting alone", 7
) %>%
mutate(across(.cols = vars, ~as_factor(.)))
# mydata %>%
# mutate(across(where(is.factor), ~fct_relabel(., str_to_sentence(.))))
Here is one possible solution. Note that the columns you are considering as
factorare actuallycharactervariables and notfactor.