title: How to count the number of components every time a graph is updated in R
body: <p>I have the following data:</p>
  <pre><code>data &lt;- data.frame(name = c(&quot;A&quot;, &quot;A&quot;, &quot;A&quot;, &quot;B&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;, &quot;D&quot;, &quot;D&quot;, &quot;D&quot;, &quot;E&quot;, &quot;B&quot;, &quot;C&quot;, &quot;C&quot;),
                   surname = c(&quot;aa&quot;, &quot;bb&quot;, &quot;cc&quot;, &quot;dd&quot;, &quot;hh&quot;, &quot;ee&quot;, &quot;ii&quot;, &quot;aa&quot;, &quot;qq&quot;, &quot;ff&quot;, &quot;gg&quot;, &quot;ff&quot;, &quot;gg&quot;, &quot;cc&quot;))
  </code></pre>
  <p>This data produces a connected graph:</p>
  <pre><code>plot(graph_from_data_frame(data, directed = F))
  </code></pre>
  <p>which obviously has 1 component.</p>
  <p>I would like to count the number of components this data produces every time we add a row in the graph. For example, the initial graph will have 1 component, since the vertices A and aa in the first row of the data are connected. The next graph will have again 1 component, since we add the second row and because of the A value in the name column. When we include the fourth row (B, dd), the graph will have 2 components.</p>
  <p>I use the following piece of code to get the number of components each time the data is updated:</p>
  <pre><code>for (i in 1:dim(data)[1]) {
  data$number_of_components[i] &lt;- components(graph_from_data_frame(data[1:i,], directed = F))$no}
  </code></pre>
  <p>Is there a smarter/more sophisticated way to get this? Thanks.</p>