I want to map a function to each combination pair of variables in a dataframe in R, returning a dataframe with the function output for each pair. I can do this manually like so:
library(tidyverse)
df <- tibble(a = c(1, 2), b = c(4, 3), c = c(5, 7))
f <- function(a, b) a - b # a simple function for sake of example
df %>% transmute(a_minus_b = f(a, b),
a_minus_c = f(a, c),
b_minus_c = f(b, c),
b_minus_a = f(b, a),
c_minus_a = f(c, a),
c_minus_b = f(c, b))
Doing this manually is obviously impractical for a dataframe with many variables. How can I apply my function to each combination pair of variables using iteration?