I have a vector which stores all possible merging combinations for a qualitative variable.
The vector has the following structure :
Config = c("4 With 1 + 6 With 2 + 8 With 3","4 With 1 With 2 + 6 With 8")
Which means that the variable with the first value of Config should have 3 modalities : Values 4 With 1 , Values 6 With 2 , Values 8 With 3
I'd like to assign a dynamic value to every element of this merging in a dynamic way, such as :
mtcars %>% mutate(Merging=case_when(carb %in% c(4,1)~"Group 1", carb %in% c(6,2)~"Group 2",carb %in% c(8,3) ~"Group 3"))
My main difficulty here is that the numbers of groups is not the same for each element of config : For one configuration, it could be 3 groups, for another one 4, etc.
I've already tried to work with string match but this possibility only works for 1 group that contains only the word "With" :
mutate(Merging= case_when(
!!sym(VAR) %in% c(unlist(str_split(Config, " With "))) ~ "Group 1",
TRUE ~ !!sym(VAR)
))
Is it a way to do it dynamically for each element of the vector and create the dedicated variable at each iteration ?
Thank you very much
is it ok