rtidyverse

Create new variable by multiplying corresponding variables and summing


I have a long list of variables that I wish to multiply by corresponding variables and sum. a_1 corresponds to b_1, a_2 to b_2 etc. The desired output would be calculated by (a_1*b_1 + a_2*b_2...)

library(dplyr)
(df <- tibble(
  a_1 = sample(1:5),
  a_2 = sample(1:5),
  b_1 = sample(1:5),
  b_2 = sample(1:5),
  desired_output = (a_1*b_1 + a_2*b_2)
))

# A tibble: 5 × 5
    a_1   a_2   b_1   b_2 desired_output
  <int> <int> <int> <int>          <int>
1     4     5     1     3             19
2     1     2     2     5             12
3     2     1     4     2             10
4     5     3     5     1             28
5     3     4     3     4             25

I have tried and failed to write a function to do this (I'm very new to trying to write functions!) e.g.

df %>%
  mutate(desired_output = function(df) {
  for (i in 1:2) {
    y1 <- get(paste0(x,'$','a_',i))
    y2 <- get(paste0(x,'$','a_',i))
    z <- y1*y2 
  }
  return(z)
}

Solution

  • One option could be:

    df %>%
     mutate(desired_output = rowSums(across(starts_with("a"), 
                                            ~ . * get(stringr::str_replace(cur_column(), "a_", "b_")))))
    
        a_1   a_2   b_1   b_2 desired_output
      <int> <int> <int> <int>          <dbl>
    1     2     3     2     5             19
    2     4     2     1     3             10
    3     3     4     5     4             31
    4     5     1     4     2             22
    5     1     5     3     1              8