1

I have created a correlation matrix with an external program (SparCC). I have calculated p-values from the same data in SparCC as well and I end up with two objects which I imported into R, let's call them corr and pval and

> ncol(corr)==nrow(corr)
[1] TRUE

> ncol(pval)==nrow(pval)
[1] TRUE

and

> colnames(corr)==rownames(pval)
[1] TRUE ...

and the same the other way around.

Since the matrices (or should I be using data.frame?) are fairly large (about 1000 items), I would like to extract the significant correlations from the corr matrix by looking up their p-value in the pval matrix, I have looked into doing something with apply:

 extracted.values <- apply(corr, nrows(corr), which(pval<0.1))

But since the part with which isn't really a function, it will output and error. Since the which command output a list of the position in the pval matrix, I'm a bit at loss as to how to retrieve the colnames and rownames for each desired items.

Is there an easier way of doing what I want, like creating a correlation object from scratch in R (is this at all possible?) which contains both corr and pval matrices and extracting the significant values? I have found this solution in Python, but a solution with R would be much appreciated if it's less complicated than what I think it is.

thanks for any help!

edit: the python example doesn't keep headers.

Community
  • 1
  • 1
adrien
  • 504
  • 1
  • 7
  • 17

1 Answers1

2

You can simply do

corr[pval < 0.1]
Pop
  • 12,135
  • 5
  • 55
  • 68
  • Thanks for your reply, that's certainly easier than what I thought, but it only outputs the array of values in `corr` that match the criteria in `pval`, how to retrieve headers as well? – adrien Mar 04 '14 at 08:58
  • Well, it turns out I do not need the headers because the output is an indexed array so for my application, the list of values suffice so this works great. – adrien Mar 05 '14 at 13:59