Reputation: 954
I want to apply a function myfoo
to every possible combination of two columns in a dataframe mydf
and get the result in a matrix format myoutput
.
Consider the following dataframe,
# Example dataframe
mydf <- data.frame(var1 = 1:10, var2 = 11:20, var3 = 21:30)
head(mydf)
# var1 var2 var3
# 1 11 21
# 2 12 22
# 3 13 23
# 4 14 24
# 5 15 25
to which I want to apply the following function to every possible combination of two columns,
# Example function
myfoo <- function(varA, varB) sum(varA * varB)
myfoo(var1, var2)
# [1] 935
in order to get this output.
# Desired output
myoutput <- matrix(c(0, 935, 1485, 935, 0, 4035, 1485, 4035, 0), 3, dimnames = list(names(mydf), names(mydf)))
myoutput
# var1 var2 var3
# var1 0 935 1485
# var2 935 0 4035
# var3 1485 4035 0
Upvotes: 2
Views: 785
Reputation: 92282
In your case I would convert to a matrix (no reason to keep it a data.frame
when all columns are of numeric class) and just run the compiled crossprod
function which does a matrix cross product.
m <- as.matrix(mydf)
res <- crossprod(m, m)
diag(res) <- 0 # You can probably skip that part
res
# var1 var2 var3
# var1 0 935 1485
# var2 935 0 4035
# var3 1485 4035 0
Upvotes: 3