2011-09-20 97 views
1

我需要將相關矩陣轉換爲Pajek輸入文件以進行網絡分析。 更具體地說,我試圖用R將相關p值的矩陣轉換爲重要相關性的「rowname columnname」列表。這是很多變量之間的重要相關性列表。 如果我有變量a,b,c,d和a,c; b,d和一,d是相關的,我想一個列表如下:將相關矩陣轉換爲R中的Pajek輸入文件

a b; 
b d; 
a d 

到目前爲止,我的[R技能不足,使我產生的相關p值矩陣,插入NA,並低於對角線(以避免無意義和重複的相關性),並且如果p值不重要/顯着,則用PALSE/TRUE替換p值。 但現在我被卡住了,一直沒有能夠谷歌我的出路。

回答

3

下面是一個例子,可能有一些最基本的幫助:

#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" 

與R中任何東西,有很多方法可以做到這樣的東西,但是這應該給你一些有用的工具一起工作。