Search code examples
rdataframesubstr

Multiply Columns by Substrings in R


Suppose I have a dataframe that has multiple components and their properties listed out in multiple columns and I want to run multiple functions against the columns. My approach was to try and base it off the substring in each column header, but I haven't been able to figure out how to do that. Below is an example of the data frame.

Basket   F_Type_1   F_Qty_1   F_P_1   F_Type_2   F_Qty_2   F_P_2 
AAA       Apple     10        2.5     Banana     9         2
BBB       Peach     5         6       Melon      20        5

I essentially want to cbind two new columns to the end of this dataframe that multiplies Qty and P so you get two new columns at the end like below.

F_Total_1   F_Total_2
25          18
30          100

The input is dynamic so on occasion it could be 2 fruits or 10 fruits in certain baskets. But I can figure that portion out, it's moreso trying to figure out how to multiply columns based on the Substrings '1' or '2'.

I appreciate all your help and any other approaches you may have!


Solution

  • We create a function that finds the specific names, and then calculates the rowwise products. The heavy lifter of this function is the mapply function. We add a final step to rename the resultant data.frame.

    fun1 <- function(data){
      qty_names <- names(data)[grepl(pattern = "Qty", x = names(data))]
    
      p_names <- names(data)[grepl(pattern = "P", x = names(data))]
    
      setNames(
        data.frame(
          mapply(qty_names, p_names, 
                 FUN = function(n1, n2) apply(data[c(n1,n2)], 1, prod))),
          paste0('F_Total_', 1:length(p_names)))
    
    
    }
    
    cbind(dat, fun1(dat))
    
      Basket F_Type_1 F_Qty_1 F_P_1 F_Type_2 F_Qty_2 F_P_2 F_Total_1 F_Total_2
    1    AAA    Apple      10   2.5   Banana       9     2        25        18
    2    BBB    Peach       5   6.0    Melon      20     5        30       100