rdplyrtidyversestringrtidytable

Dynamic mutate case_when according to string matching


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


Solution

  • is it ok

    library(dplyr)
    library(stringr)
    library(purrr)
    library(rlang)
    
    # Sample data
    Config <- c("4 With 1 + 6 With 2 + 8 With 3", "4 With 1 With 2 + 6 With 8")
    mtcars <- data.frame(carb = c(4, 1, 2, 6, 8, 3)) # Sample data
    
    # Function to create dynamic conditions
    create_conditions <- function(config) {
      pairs <- str_split(config, "\\s*\\+\\s*")[[1]]
      
      conditions <- map2(pairs, 1:length(pairs), function(pair, group_num) {
        values <- as.numeric(str_extract_all(pair, "\\d+")[[1]])
        group_name <- paste("Group", group_num)
        condition <- expr(carb %in% c(!!!values) ~ !!group_name)
        return(condition)
      })
      
      return(conditions)
    }
    
    # Create a list of conditions for each element of Config
    conditions_list <- map(Config, create_conditions)
    
    # Create a dynamic column using case_when
    mtcars <- mtcars %>%
      mutate(Merging = case_when(!!!set_names(unlist(conditions_list), NULL)))
    
    # View the result
    print(mtcars)