[R] cor.test in matrices

juli g. pausas juli at ceam.es
Wed Mar 5 22:34:16 CET 2003

For computing correlation among variables in a matrix, I use cor( ), but 
for computing the p-values I'm using cor.test in the following way:

cor.p <- function(X)
res <- matrix(0, ncol(X), ncol(X))
for (i in 1:ncol(X))
for (j in 1:ncol(X)) res[i, j]<- cor.test(X[, i], X[, j])$p.value
rownames(res) <- colnames(res) <- colnames(X)

I'm just wondering if there is a better (nicer) way to do the same.
For example, would it be possible to use apply() with cor.test instead 
of using the for loops?
I'm trying to improve my low R skills.


More information about the R-help mailing list