Here is a simple way to handle two data frames. First make some data:
set.seed(42)
A <- data.frame(matrix(rnorm(100), 25))
B <- data.frame(A + matrix(rnorm(100, 0, .1), 25))
Now create vectors of the row and column indices and use mapply
to make a list:
a <- rep(seq_len(ncol(A)), ncol(B))
b <- rep(seq_len(ncol(B)), each=ncol(B))
all <- mapply(function(x, y) cor.test(A[, x], B[, y]), a, b, SIMPLIFY=FALSE)
The list includes all of the information for each correlation test, e.g.
all[[1]]
# Pearson's product-moment correlation
#
# data: A[, x] and B[, y]
# t = 61.1, df = 23, p-value <2e-16
# alternative hypothesis: true correlation is not equal to 0
# 95 percent confidence interval:
# 0.99295 0.99867
# sample estimates:
# cor
# 0.99694
Now extract just the correlations and p.values. As noted, the rows are A and the columns are B:
# Results with A=rows, and B=columns
corrs <- matrix(sapply(x, "[[", "estimate"), ncol(A))
pvals <- matrix(sapply(x, "[[", "p.value"), ncol(A))
round(corrs, 4)
# [,1] [,2] [,3] [,4]
# [1,] 0.9969 -0.0870 -0.3249 -0.0690
# [2,] -0.0582 0.9964 -0.1256 -0.1166
# [3,] -0.3216 -0.1456 0.9955 0.1324
# [4,] -0.0765 -0.1497 0.1411 0.9958
round(pvals, 4)
# [,1] [,2] [,3] [,4]
# [1,] 0.0000 0.6794 0.1130 0.7431
# [2,] 0.7822 0.0000 0.5498 0.5788
# [3,] 0.1169 0.4873 0.0000 0.5282
# [4,] 0.7161 0.4750 0.5010 0.0000