You may want to look into fast distance covariance and correlation
which I think R probably has an implementation of.
The answers above have details for R programming to get the
Pearson correlation coefficient which is what you asked for.
To explore distance correlation here is a quick summary of
some steps you could take. The code is in java, but you could port
it to R.
To only calculate the correlation between the the same column
in each of the 2 datasets of the
same size and number of variables, matrix1 and matrix2,
you are calculating only the diagonal of a full correlation matrix.
And you are wanting to combine those correlations into
one number to use as a replacement for similarity between the
2 matrices.
let x1 = matrix1 and x2 = matrix2 below
input: double[][] x1 and double[][] x2 being same size
double[][] dCor = zeros(x1[0].length, x2[0].length);
double[] tmp1 = new double[x2.length];
double[] tmp2 = new double[x2.length];
int i, j, k;
for (i = 0; i < x1[0].length; ++i) {
//for (j = 0; j < x2[0].length; ++j) {
j = i
// copy each column vector to calculate correlation
for (k = 0; k < x2.length; ++k) {
tmp1[k] = x2[k][j];
tmp2[k] = x1[k][i];
}
// fast dcor runtime is O(n*log_2(n))
dCor[i][j] = sqrt(fastDCor(tmp1, tmp2).corSq);
//}
}
where fastDCor is fast distance correlation.
reference:
"A fast algorithm for computing distance correlation"
2019 Chaudhuri and Hu, Computational Statistics And Data Analysis,
Volume 135, July 2019, Pages 15-24.
For combining correlation0 with correlation 1 and correlation2 etc.
you could browse the short paper:
Charter and Alexander 1993, "A Note on Combining Correlations".
The paper details are for the Pearson correlation, but are informative.
cor(as.vector(matrix1), as.vector(matrix2))
? $\endgroup$