-1

I have tried to compute the number of neighbors in common between two vertices.

the test file contain

1 2

1 4

1 5

2 3

2 4

2 5

3 4

y<-read.table("test.txt")

require(igraph)

g<-graph.data.frame(y, directed=F, vertices=NULL)

for(i in 1:5)
{
  for(j in 1:5)
{

c[i,j]<-cocitation(g,i)[j]

}}

y$neigC<-c[y$V1,y$V2]

But, when I try to add it to a data frame, it fails to give me the correct answer.

Camilla
  • 117
  • 1
  • 10

1 Answers1

1

Try this :

y<-structure(list(V1 = c(1L, 1L, 1L, 2L, 2L, 2L, 3L),
                  V2 = c(2L, 4L, 5L, 3L, 4L, 5L, 4L)),
             .Names = c("V1", "V2"), class = "data.frame", row.names = c(NA, -7L))

require(igraph)
g<-graph.data.frame(y, directed=F, vertices=NULL)
d<-cocitation(g)
y[,3]<-sapply(1:nrow(y),function(x){d[y[x,1],y[x,2]]}) # or diag(d[y[,1],y[,2]])

 y
  V1 V2 V3
1  1  2  2
2  1  4  1
3  1  5  1
4  2  3  1
5  2  4  2
6  2  5  1
7  3  4  1

here is g :

enter image description here

and d (I renamed it) looks like this :

unname(d)
     [,1] [,2] [,3] [,4] [,5]
[1,]    0    2    2    1    1
[2,]    2    0    1    2    1
[3,]    2    1    0    1    1
[4,]    1    2    1    0    2
[5,]    1    1    1    2    0

was that what you expected ?

etienne
  • 3,648
  • 4
  • 23
  • 37
  • thanks it works but can we use structure dynamically. Specifically when we have a data frame that contains more of 1000 observation. – Camilla Nov 16 '15 at 17:11
  • what do you mean by using `structure` ? Is this about `y` ? (if it is it was just to make the answer reproducible) – etienne Nov 16 '15 at 17:12