Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.6k views
in Technique[技术] by (71.8m points)

r - How to insert the results regarding similarity measures (igraph) in a specific table?

I have a edge list for which I want to determine similarity measures using R.

Edge list

Table 1

c1       c2
Algeria Andorra
Algeria Angola
Algeria Antigua and Barbuda
Algeria Argentina
Algeria Armenia
Algeria Australia
Algeria Austria
Algeria Azerbaijan
Algeria Bahamas
Algeria Bahrain
Algeria Bangladesh
Algeria Barbados
Algeria Belarus
Andorra USA

I am using igraph

library (igraph)

data<- read.csv(file.choose(), header =TRUE) 

network<- graph.data.frame(data, directed = FALSE)

results<- ( similarity(
  network,
   vids = V(network),
   mode = c("all"),
   loops = FALSE,
   method = c("jaccard")))

From this, I get the similarity index as

Table 2

V1  V2  V3  V4  V5  V6  V7  V8  V9  V10 V11 V12 V13 V14 V15
1   0   0   0   0   0   0   0   0   0   0   0   0   0   0.0769
0   1   0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0
0   0.5 1   1   1   1   1   1   1   1   1   1   1   1   0
0   0.5 1   1   1   1   1   1   1   1   1   1   1   1   0
0   0.5 1   1   1   1   1   1   1   1   1   1   1   1   0
0   0.5 1   1   1   1   1   1   1   1   1   1   1   1   0
0   0.5 1   1   1   1   1   1   1   1   1   1   1   1   0
0   0.5 1   1   1   1   1   1   1   1   1   1   1   1   0
0   0.5 1   1   1   1   1   1   1   1   1   1   1   1   0
0   0.5 1   1   1   1   1   1   1   1   1   1   1   1   0
0   0.5 1   1   1   1   1   1   1   1   1   1   1   1   0
0   0.5 1   1   1   1   1   1   1   1   1   1   1   1   0
0   0.5 1   1   1   1   1   1   1   1   1   1   1   1   0
0   0.5 1   1   1   1   1   1   1   1   1   1   1   1   0
0.0769  0   0   0   0   0   0   0   0   0   0   0   0   0   1



Now, I would like to have this information as follows:

The similarity index for all possible pairs.

Table 3

c1       c2                jaccard_index
Algeria  Andorra                    0
Algeria  Angola                     0
Algeria  Antigua and Barbuda        ... 
Algeria  Argentina  
Algeria  Armenia    
Algeria  Australia  
Algeria  Austria    
Algeria  Azerbaijan 
Algeria  Bahamas    
Algeria  Bahrain    
Algeria  Bangladesh 
Algeria  Barbados   
Algeria  Belarus
Algeria  USA        
Andorra  USA    
Andorra  Angola 
Andorra  Antigua and Barbuda    
Andorra  Argentina  
Andorra  Armenia    
Andorra  Australia  
Andorra  Austria    
Andorra  Azerbaijan 
Andorra  Bahamas    
Andorra  Bahrain    
Andorra  Bangladesh 
Andorra  Barbados   
Andorra  Belarus    
....     ...         ....

Is there a simple way to get the information as in Table 3?

Any tips appreciated.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Something like this should do it,

colnames(results) <- rownames(results) <- attr(V(network), 'names')
d <- reshape2::melt(results, varnames = c('c1', 'c2'), value.name = 'jaccard_index')
d

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...