2014-07-22 94 views
0

我試圖計算一些網絡措施R使用圖片 apckage。 起初我讀取我的數據,而不是轉換它,並最終將它帶入鄰接矩陣形式。然後我計算這個數據集的度數值。R:循環社交網絡分析

我想將我的代碼放在一個循環中,所以我不需要手動更改條件 - 重新讀取數據 - 並再次運行代碼。但我沒有運氣。 「r」條件可以是「1」或「2」,「g」條件是從「1」到「21」的數字。

我的代碼看起來像這樣至今:

p1 <- read.csv("matrix.csv") 
p <- p1[p1$r=="1" & p1$g == "1",] 
rownames(p) <- paste("id", p$id, sep=".") 
p <- t(p[,-(1:3)]) 
p <- data.frame(p) 
rr <- apply(p, 2, as.character) 

m<-matrix(nrow=ncol(rr),ncol=ncol(rr)) 
for(col in 1:ncol(rr)){ 
    matches<-rr[,col]==rr 
    match.counts<-colSums(matches) 
    match.counts[col]<-0 
    m[,col]<-match.counts 
} 

n<-graph.adjacency(m) 

d<-degree(n, v=V(n)) 

第一「P1」的幾行:

1> p1 
    id  g r X1  X2   X3  X4 
1 1  1 1 1324;1256 1324;1256 752;1268 1892;1236 
2 2  1 2 324;988 324;988 324;988 324;988 
3 3  1 1 1312;1652 1312;1652 1828;608 712;656 
4 4  1 2 324;988 324;988 324;988 324;988 ... 

我知道我的代碼是相當難看......我之前沒有任何編程經驗,但我渴望學習,所以我歡迎任何建議或建議。

在此先感謝!

+0

請包括自包含代碼。我們不知道'matrix.csv'中的內容。 –

+0

我在「matrix.csv」中保存的是「p1」。它包含252行和44列。前3列是識別的,其他的是座標,我用它來製作鄰接矩陣。 – Sielu

回答

1

這裏我假設前3列是標識符,後面的任何列描述的格式爲V1;V2,其中V1V2是頂點標識。

只是把你的數據框的一小部分,這是我想到的。我不認爲你需要創建一個鄰接矩陣,因爲你可以創建一個邊界列表。

require(igraph) 
p1 <- read.csv(textConnection(
"id,g,r,X1,X2,X3,X4                                         
1,1,1,1324;1256,1324;1256,752;1268,1892;1236                                          
2,1,2,324;988,324;988,324;988,324;988                                            
3,1,1,1312;1652,1312;1652,1828;608,712;656                                           
4,1,2,324;988,324;988,324;988,324;988")) 

要爲R和G的一個值做到這一點:

p <- p1[p1$r=="1" & p1$g == "1",] ## limit to only rows where r and g are 1 

myEdges <- p[,-(1:3)] ## assuming edges are defined in all columns after the first 3                                     
dat <- apply(myEdges, 1, function(strings) unlist(strsplit(strings, ';', fixed=TRUE))) 
myGraph <- graph.data.frame(dat, directed=FALSE) # can change to directed by setting directed = TRUE 
plot(myGraph) # see what the graph looks like, don't try if graph is large! 

degree(myGraph) 
# 1324 1256 752 1268 1892 1236 1312 1652 1828 608 712 656 
# 2 2 1 1 1 1 2 2 1 1 1 1 

要獲得關於自動化過程R和G的不同組合評論,你可以使用的方法類似這樣的嵌套for循環(效率不高,但可能會因您的問題而有所不同)

rVals <- 1:2 
gVals <- 1:21 
myGraphList <- rep(list(vector(mode = "list", length = length(gVals))), length(rVals)) 
for(r in rVals) { 
    for(g in gVals) { 
    p <- p1[p1$r == r & p1$g == g,] ## limit to only certain values of r and g                                 
    myEdges <- p[,-(1:3)] ## assuming edges are defined in all columns after the first 3                              
    if(nrow(myEdges) > 0) { ## Only need to create a graph if there are edges present                               
     dat <- apply(myEdges, 1, function(strings) unlist(strsplit(strings, ';', fixed=TRUE))) 
     myGraphList[[r]][[g]] <- graph.data.frame(dat, directed=FALSE) 
    } 
    } 
} 

## Only 2 elements with an igraph object in this small example:                                     
degree(myGraphList[[1]][[1]]) # when r == 1, g == 1                                        
# 1324 1256 752 1268 1892 1236 1312 1652 1828 608 712 656                                     
# 2 2 1 1 1 1 2 2 1 1 1 1                                     
degree(myGraphList[[2]][[1]]) # when r == 2, g == 1                                        
# 324 988                                                  
# 8 8 
+0

謝謝!我想知道如果我能以某種方式使限制 'p < - p1 [p1 $ r ==「1」&p1 $ g ==「1」,]''自動?所以我不必手動更改數字?要條件r ==「1」,我有條件g ==「1:21」,並且對於r ==「2」,我再次擁有g =「1:21」。 – Sielu

+0

非常感謝!你的回答非常有幫助!這只是我尋找的解決方案! :) – Sielu