[R] applying cor.test to a (m, n) matrix

From: Monica Pisica <pisicandru_at_hotmail.com>
Date: Thu, 08 May 2008 19:05:53 +0000

Hi everybody,

I would like to apply cor.test to a matrix with m rows and n columns and get the results in a list of matrices , one matrix for p.val, one for the statistic, one for the correlation and 2 for upper and lower confidence intervals, something analog with cor() applied to a matrix.

I have done my own function to get a matrix of p.values and i suppose i can build similar functions for all the others. But i have used for loops and i wonder if there is any way to actually use one of the functions from the "apply" family to do this in a quicker way.

Here is my little function:

cor.pval <- function(x, method = c("pearson", "kendal", "spearman"), digit=8) { n <- dim(x)[2]
pval <- matrix(paste(rep("c", n*n), seq(1,n*n), sep = ""), n, n, byrow = T) for (i in 1:n) {

        for (j in 1:n){
pval[i, j] <- cor.test(x[,i], x[,j], method = method)$p.value


pval <- matrix(round(as.numeric(pval),digit), n, n, byrow = T) rownames(pval) <- colnames(x)
colnames(pval) <- colnames(x)

Thanks for any input,



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Thu 08 May 2008 - 19:07:56 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 09 May 2008 - 07:30:36 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive