Sometimes I want to use a double for loop with an index to columns in a matrix, compute some value between them and assign to a cell in a matrix. A correlation table is an example of this. I was wondering if/how this can be done in data.table syntax. Here's the example as a for loop. How can I do the same thing in *data.table** even if it is slower this is more can it be done though faster would be nice. Note that we can't assume the value computer will give a symmetric matrix (i.e., y[i, j]
!=
y[j, i]
necessarily).
cos_sim <- function(x, y) x %*% y / sqrt(x%*%x * y%*%y)
x <- mtcars
y <- matrix(, nrow = ncol(x), ncol = ncol(x))
for (i in 1:ncol(x)) {
for (j in 1:ncol(x)) {
y[i, j] <- cos_sim(x[, i], x[, j])
}
}
library(data.table)
x <- as.data.frame(x)
setDT(x)