title: Efficient way to analysis neighbours of subsets of nodes in large graph
body: <p>I have a graph of 6 million of nodes such as </p>
  
  <pre><code>require(igraph)
  
  # Graph of 1000 nodes
  g &lt;- ba.game(1000)
  </code></pre>
  
  <p>with the following four attributes defined for each node</p>
  
  <pre><code># Attributes
  V(g)$attribute1 &lt;- V(g) %in% sample(V(g), 20)
  V(g)$attribute2 &lt;- V(g) %in% sample(V(g), 20)
  V(g)$attribute3 &lt;- V(g) %in% sample(V(g), 20)
  V(g)$attribute4 &lt;- V(g) %in% sample(V(g), 20)
  </code></pre>
  
  <p>Among the nodes I have a <code>subset</code> of 12,000 that are of particular interest:</p>
  
  <pre><code># Subset of 100 nodes
  V(g)$subset &lt;- V(g) %in% sample(V(g), 100)
  </code></pre>
  
  <p>What I want to obtain is an analysis (count) of the neighbourhood of my <code>subset</code>. That is, I want to define</p>
  
  <pre><code>V(g)$neigh.attr1 &lt;- rep(NA, vcount(g))
  V(g)$neigh.attr2 &lt;- rep(NA, vcount(g))
  V(g)$neigh.attr3 &lt;- rep(NA, vcount(g))
  V(g)$neigh.attr4 &lt;- rep(NA, vcount(g))
  </code></pre>
  
  <p>such that <code>NA</code> is replaced for every node in the <code>subset</code> with the corresponding count of neighbouring nodes with <code>V(g)$attribute{1..4}==TRUE</code>.</p>
  
  <p>I can easily create a list of the neighbourhood of interest with </p>
  
  <pre><code>neighbours &lt;- neighborhood(g, order = 1, V(g)[V(g)$subset==TRUE], mode = "out")
  </code></pre>
  
  <p>but I can't think of an efficient way to iterate over every <code>neighbours</code> and compute the statistics for each of the four attributes. Indeed the only way I've came up with is a loop which given the size of my original graph takes just too long:</p>
  
  <pre><code>subset_indices &lt;- as.numeric(V(g)[V(g)$subset==TRUE])
  for (i in 1:length(neighbours)) {
      V(g)$neigh.attr1[subset_indices[i]] &lt;- sum(V(g)$attribute1[neighbours[[i]]])
      V(g)$neigh.attr2[subset_indices[i]] &lt;- sum(V(g)$attribute2[neighbours[[i]]])
      V(g)$neigh.attr3[subset_indices[i]] &lt;- sum(V(g)$attribute3[neighbours[[i]]])
      V(g)$neigh.attr4[subset_indices[i]] &lt;- sum(V(g)$attribute4[neighbours[[i]]])
  }
  </code></pre>