I was looking at your question again, and it appears that you want a matrix X
with the same dimensions of mat2
, where X[i,j]
is given by the number of complete cases in mat1[,c(i,j)]
. Then mat2
will be multiplied by X
.
The number of complete cases is given by sum(complete.cases(mat1[,c(i,j)]))
. I want to use this in outer
which requires a vectorized function, so this is passed through Vectorize
:
outer(seq(nrow(mat2)), seq(ncol(mat2)),
Vectorize(function(x,y) sum(complete.cases(mat1[,c(x,y)])))
)
## [,1] [,2] [,3]
## [1,] 2 2 1
## [2,] 2 3 1
## [3,] 1 1 1
This is your desired symmetric matrix.