I'm working on a survey research project in which we need to multiply one group of columns against another group of columns. I can do this easily in base R, but I can't figure out how to do it within a tidy, pipe-based workflow. I found many solutions for multiplying a set of columns against one column, but not for multiple columns. Any help is greatly appreciated.
The example below demonstrates how I solve the problem in base R.
library(tidyverse)
df <- tibble(
a1 = c(1, 2, 3, 4, 5),
a2 = c(5, 4, 3, 2, 1),
a3 = c(1, 5, 2, 4, 3),
b1 = c(1, 1, 2, 1, 1),
b2 = c(3, 3, 5, 4, 1),
b3 = c(2, 1, 4, 2, 1)
)
new_df <- df[, c("a1", "a2", "a3")] * df[, c("b1", "b2", "b3")]
names(new_df) <- c("c1", "c2", "c3")
Created on 2022-06-14 by the reprex package (v2.0.1)