Extract values from a correlation matrix according to their p-value in a second matrix

淺唱寂寞╮ 提交于 2020-03-03 10:14:44

问题


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.


回答1:


You can simply do

corr[pval < 0.1]


来源:https://stackoverflow.com/questions/22166604/extract-values-from-a-correlation-matrix-according-to-their-p-value-in-a-second

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!