title: How do I delete nodes in igraph in R?
body: <p>I'm trying to write a script that deletes nodes in a Barabasi-Albert Network in R using igraph based on the node's number of connections (I'm trying to recreate some basic results from the "Error and attack tolerance of complex networks" paper by Albert, Jeong and Barabasi). I'm starting by trying to delete five random nodes that have less than the average number of connections of a node in the network. However, when I visualize the network after I attempt to delete the nodes, it does look different, but it doesn't look like the nodes are removed. So I'm not convinced the script is working. </p>
  
  <pre><code>nnodes=50 #number of nodes
  test.graph&lt;-barabasi.game(nnodes,.5) #create B-A network
  test.graph2=test.graph #create a second B-A network for removing nodes
  bar_mat=matrix(0,nrow=nnodes,ncol=1) #create empty matrix
  for (i in 1:nnodes){
  bar_mat[i,]=sum(test.graph[,i]) #sums up the number of connections of each node
  }
  ###Visualizing the network before removing nodes
  barabasi.community&lt;-walktrap.community(test.graph) #this is supposed to visualize the most
  #connected nodes in the network
  members&lt;-membership(barabasi.community)
  plot.igraph(test.graph,
          layout=layout.fruchterman.reingold,
          vertex.size=10,
          vertex.label.cex=.5,
          edge.arrow.size=.5,
          mark.groups=list(members),
          mark.col="green"
  )
  f=c()
  
  for (k in 1:5){ #checking five random nodes
   a=sample(1:nrow(bar_mat),1) #select random node
   if(bar_mat[a,]&lt;=mean(bar_mat)){
    test.graph2 &lt;- delete.vertices(test.graph2,a) # this is supposed to delete 
    #the node based on if it has lower than the average amount of connections
    i=i+1 #counting how many nodes of the five are actually removed
   }
  f[k]=a #putting the nodes tested into a vector
  a=0 #resetting a
  }
  ###Visualizing network after node removal 
  barabasi.community2&lt;-walktrap.community(test.graph2)
  members2&lt;-membership(barabasi.community2)
  plot.igraph(test.graph2,
          layout=layout.fruchterman.reingold,
          vertex.size=10,
          vertex.label.cex=.5,
          edge.arrow.size=.5,
          mark.groups=list(members2),
          mark.col="pink"
  ) 
  </code></pre>
  
  <p>The script runs when the number of nodes is smaller (like around 50) but when the number of nodes is higher (around 100) I get the following error:</p>
  
  <pre><code>Error in delete.vertices(test.graph2, a) : 
  At iterators.c:759 : Cannot create iterator, invalid vertex id, Invalid vertex id
  </code></pre>
  
  <p>I think it has to do with the naming conventions of the nodes, but I'm not sure. I'm new to network science and I'm not the best programmer out there so I would really appreciate any help. Thanks!</p>