2017-03-03 109 views
-1

我有一個數據集幾個重疊的標準和它們發生的頻率。我想使用R circlize包將數據繪製爲網絡(和絃)圖。我試圖將數據轉換爲鄰接矩陣而沒有成功。我可以將發生成對的觀察結果轉換爲矩陣。但是,當有超過兩個標準在一起時,我無法做到。 數據集可以訪問here轉換一個數據幀,以鄰接矩陣中的R

的數據看起來像這樣

criteria criteria1 criteria2 criteria3 criteria3 Frequency 
None     151 
G     121 
BH     108 
KBA     4 
IBA KBA    172 
AZE KBA    1 
AZE IBA KBA   3 
G KBA    6 
G IBA KBA   129 
G AZE KBA   3 
G AZE KBA IBA  7 
BH KBA    7 
BH IBA KBA   121 
BH AZE KBA   6 
BH AZE IBA KBA  15 
BH G    153 
BH G KBA   32 
BH G IBA KBA  200 
BH G AZE   5 
BH G AZE KBA  4 
BH G AZE IBA KBA 44 
+0

當超過2個標準時,你認爲每對組合都是邊緣嗎? – Marcelo

+0

馬塞洛,是的!每一對都將被視爲一個優勢。 – Arihant

回答

1

你必須讓所有在這裏你有一個以上的標準以及頻率分配給它的行一對組合。然後你總結同一邊緣這裏的頻率是代碼:

require(dplyr) 

#Helper fucntion to get pairwise conbinations of criteria 
getEdges <- function(x) 
{ 
     # simplify the list 
     v<-unlist(x); 

     #Get the pairs and create a dataframe with the frequencies 
     cb<-combn(v[1:length(v)-1],2, simplify=F); 
     df<-data.frame(matrix(unlist(cb),ncol=2,byrow=T),frequency=as.integer(v[length(v)]),stringsAsFactors=F); 

     return (df) 
} 

#Get the pairs 
edges <- lapply(split(df, seq(nrow(df))), getEdges) 

#join the list into one dataframe 
edges<-bind_rows(edges) 

#Remove empty source and destination 
edges <-edges[edges$X1!=""&edges$X2!="",] 

#aggregate on edges 
aggr <- aggregate(edges$frequency,by=list(edges$X1,edges$X2), FUN=sum) 

據幀aggr是邊緣的列表。

+0

馬塞洛,謝謝!繪製和絃圖後,數字看起來非常高,我意識到這些對不能被視爲邊緣。觀測總數(頻率總和)不能超過1292。 – Arihant