Search code examples
rdplyrmutate

Iterating a mutate function across different variables in a dataframe


I need a mutate and iterate function that will combine 6 variables(1st to 6th, 7th to 12th…..) and create 1 new combined variable using the sample data set below:

Sample data frame with 75 variables (test 75)

test75 <- data.frame(matrix(sample(0:1, 75 * 6, replace = TRUE), ncol = 75))
names(test75) <- paste0("Q", 1:75)

Solution

  • #I tried in vain earlier but I found a solution which I have posted below for the same data set.

    library(dplyr)
    library(tidyverse)
    library(janitor) 
    
    # Sample dataset with a data frame with 75 variables (test 75)
    test75 <- data.frame(matrix(sample(0:1, 75 * 6, replace = TRUE), ncol = 75))
    names(test75) <- paste0("Q", 1:75)
    view(test75)
    
    # 1. Define the transformation function
    apply_transformations <- function(df, start) {
      if (start + 5 <= ncol(df)) {  # Ensure there is group of 6 to process
        vars_in_group <- names(df)[start:(start+5)]
        combined_name <- paste0("Q", start, "Combined")
        
        df <- df %>%
          mutate(!!combined_name := case_when(
            !!sym(vars_in_group[1]) == 1 ~ 5,
            !!sym(vars_in_group[2]) == 1 ~ 4,
            !!sym(vars_in_group[3]) == 1 ~ 3,
            !!sym(vars_in_group[4]) == 1 ~ 2,
            !!sym(vars_in_group[5]) == 1 ~ 1,
            !!sym(vars_in_group[6]) == 1 ~ 99,
            TRUE ~ 0
          ))
      }
      return(df)
    }
    
    # 2. Calculate appropriate indices to start each group
    indices <- seq(1, ncol(test75) - 6, by = 8)
    
    # 3. Apply the function to each group starting index
    for (index in indices) {
      test75 <- apply_transformations(test75, index)
    }
    
    # Print the resulting data frame to see some of the combined variables
    print(head(test75))
    view(test75)