First of all, thanks for spending the time to look at this.
I actually found a better solution and I wanted to post it here. In the previous example, it did work because both matrices were composed of single values. But the function is actually not working as it should. It should mirror the lower triangle of the second matrix in the upper triangle of the first one.
You can check this simulating matrices with difference values:
mat1 <- matrix(nrow = 10, ncol = 10)
mat2 <- matrix(nrow = 10, ncol = 10)
mat1[lower.tri(mat1)] <- runif(n = 10, min = 0, max = 1)
mat2[lower.tri(mat2)] <- runif(n = 10, min = 0, max = 1)
fx <- function(mat1, mat2) {
n <- nrow(mat1)
for (i in 1:n) {
for (j in 1:n) {
if (i > j) {
mat1[j, i] <- mat2[i, j]
}
}
}
mat1
}
mat3 <- fx(mat1, mat2)
I suspect there must be something in base R to do this kind of work... In any case, you could see that in mat3 now the upper triangle corresponds to t(mat2).
Cheers,