Extract values from a correlation matrix according

2019-07-30 15:01发布

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条回答
仙女界的扛把子
2楼-- · 2019-07-30 15:26

You can simply do

corr[pval < 0.1]
查看更多
登录 后发表回答