I'm trying to create new columns in a data frame from the products of multiplying one selection of columns with another. E.g:
df <- as.data.frame(matrix(rep(1:6, 3), nrow = 3,
dimnames = list(NULL, letters[1:6])))
df
A data.frame: 3 × 6
a b c d e f
1 4 1 4 1 4
2 5 2 5 2 5
3 6 3 6 3 6
df <- df %>% mutate(df$a:df$c * df$d:df$f)
df
A data.frame: 3 × 6
a b c d e f a*d a*e a*f b*d b*e b*f c*d c*e c*f
1 4 1 4 1 4 4 1 4 16 4 16 4 4 16
2 5 2 5 2 5 10 4 10 25 10 25 10 4 10
3 6 3 6 3 6 12 9 12 36 18 36 18 9 18
I want to find an easy general way to create product columns and add to the dataset.
In the example I try to multiply columns a, b and c with columns d, e and f and add all possible combinations to the dataframe. The syntax above obviously doesn't work, so I want to find the easiest solution to accomplish this.