I have multiple Tibbles and I would like to be mutate them all with a function. The issue is that the column names are partially different: That is what I want to apply
mpg %>% mutate(model_QQ = if_else(year == 1999, 0 , if_else(cty > 20, 1, -1)))
But the tibbles have colnames like:
c("audi","nissan","subaru") %>% set_names() %>% map(~mpg %>% rename_at(vars(model:class), list(~str_c(.,"_",!!quo(.x)))))
I was thinking to make something like:
mpg %>% mutate(model_QQ = if_else(starts_with("year") == 1999, 0 , if_else(starts_with("cty") > 20, 1, -1)))
but these are working only inside the select function. Any suggestions?
EDIT I remake the code with case when, maybe is made more clear that way about what I'm looking for:
mpg %>%
mutate(
model_QQ = case_when(
starts_with("year") == 1999 ~ 0L,
starts_with("cty") > 20 ~ 1L,
starts_with("cty") <= 20 ~ -1L
))