简体   繁体   English

将相关矩阵转换为R中的Pajek输入文件

[英]Convert a correlation matrix to a Pajek input file in R

I need to convert a correlation matrix to a Pajek input file for networks analysis. 我需要将相关矩阵转换为Pajek输入文件以进行网络分析。 More specifically I'm trying to use R to convert a matrix of correlation p-values to a list of ”rowname columnname” for the significant correlations. 更具体地说,我正在尝试使用R将相关性p值的矩阵转换为重要相关性的“行名列名”列表。 That is a list of each of the significant correlations between quite a lot of variables. 这是很多变量之间的每个重要相关性的列表。 If I have variables a,b,c,d and a,c; 如果我有变量a,b,c,d和a,c; b,d and a,d were correlated, I'd like a list as follows: b,d和a,d是相关的,我想要一个列表,如下所示:

a b;
b d;
a d

So far my inadequate R skills have enabled me to generate a correlation p-value matrix, to insert NA on and below the diagonal (to avoid meaningless and duplicated correlations) and to substitute the p-values with FALSE/TRUE if the p-value is non-significant/significant. 到目前为止,我不足的R技能使我能够生成相关的p值矩阵,在对角线上方和下方插入NA(以避免无意义和重复的相关性),并且如果p值将P值替换为FALSE / TRUE不重要/不重要。 But now I am stuck and have not been able to google my way out of it. 但是现在我被困住了,无法用谷歌搜索。

Here's an example that may help with some basics: 这是一个可能有助于一些基础知识的示例:

#Create a matrix
m <- matrix(1:16,4,4)
rownames(m) <- letters[1:4]
colnames(m) <- letters[1:4]
m
  a b  c  d
a 1 5  9 13
b 2 6 10 14
c 3 7 11 15
d 4 8 12 16

#Identify the indices for entries in m
# that are greater than 10 
m1 <- which(m > 10, arr.ind = TRUE)

#Row and column names of those entries
# greater than 10. Notice the use of subsetting
# via [. 
cbind(rownames(m)[m1[,1]],colnames(m)[m1[,2]])
     [,1] [,2]
[1,] "c"  "c" 
[2,] "d"  "c" 
[3,] "a"  "d" 
[4,] "b"  "d" 
[5,] "c"  "d" 
[6,] "d"  "d" 

As with anything in R, there are many ways to do stuff like this, but this should give you some useful tools to work with. 与R中的任何内容一样,有很多方法可以执行此类操作,但这应为您提供一些有用的工具。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM