Daniel - 1 year ago 41

R Question

I am working with

`igraph`

`library("igraph")`

intra.edges<-function(G,algorithm) {

if(algorithm==1){

Mod<-cluster_louvain(G)}

if(algoritmo==2){

Mod<-cluster_edge_betweenness(G)}

if(algoritmo==3){

Mod<-cluster_walktrap(G)}

Com<-as.data.frame(sizes(Mod))

NoCom<-as.vector(Com$Community.sizes)

vert<-NULL

for(i in 1:length(NoCom)){

M<-which(membership(Mod)==i)

sg<-induced.subgraph(G,M)

c.ec<-ecount(sg)

vert<-c.ec

}

intra<-data.frame(Com,vert)

print(intra)

}

When I try the function, it don't works correctly. For example:

When I run:

`G <- graph.famous("Zachary")`

intra.edges(G,1)

I get:

`Community.sizes Freq vert`

1 9 6

2 7 6

3 9 6

4 4 6

5 5 6

And when I run

`intra.edges(G,2)`

`intra.edges(G,3)`

Also, not all the network's components have six vertex, it is only in one component.

Answer Source

You can either add your calculated value of `vert`

to the dataframe with each iteration of your for loop by changing your code to:

```
intra<-Com
for(i in 1:length(NoCom)){
M<-which(membership(Mod)==i)
sg<-induced.subgraph(G,M)
intra$vert[i]<-ecount(sg)
}
print(intra)
```

Or, as @dash2 suggested, create a vector called `vert`

and add values sequentially like this:

```
vert<-NULL
for(i in 1:length(NoCom)){
M<-which(membership(Mod)==i)
sg<-induced.subgraph(G,M)
c.ec<-ecount(sg)
vert[i]<-c.ec
}
```