2017-03-03 112 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