title: Count how many vertices in a vertex's neighbourhood have an attribute in igraph for R
body: <p>I have a large graph (several, actually) in igraph—on the order of 100,000 vertices—and each vertex has an attribute which is either <code>true</code> or <code>false</code>. For each vertex, I would like to count how many of the vertices directly connected to it have the attribute. My current solution is the following function, which takes as its argument a graph.</p>
  
  <pre><code>attrcount &lt;- function(g) {
    nb &lt;- neighborhood(g,order=1)
    return(sapply(nb,function(x) {sum(V(g)$attr[x]}))
  }
  </code></pre>
  
  <p>This returns a vector of counts which is off by 1 for vertices which have the attribute, but I can adjust this easily.</p>
  
  <p>The problem is that this runs incredibly slowly, and it seems like there should be a fast way to do this, since, for instance, computing the degree of each vertex is practically instantaneous with <code>degree(g)</code>.</p>
  
  <p>Am I doing this a stupid way?</p>
  
  <p>As an example, suppose this was our graph.</p>
  
  <pre><code>set.seed(42)
  g &lt;- erdos.renyi.game(169081, 178058, type="gnm")
  V(g)$att &lt;- as.logical(rbinom(vcount(g), 1, 0.5))
  </code></pre>