title: Counting polygons in shapefile
body: <p>So I have multiple species ranges which look like the following (colored blue for example) This one runs east to west across Africa:</p>
  
  <p><img src="https://i.stack.imgur.com/y81Tv.png" alt="Example species range"></p>
  
  <p>I can get the total area by using <code>gArea</code> in the <code>rgeos</code> package. What I want to know is <em>how many individual polygons make up this file</em> - i.e. how many distinct regions are there to the total range (this could be islands, or just separated populations) and <em>what the ranges of those polygons are</em>. I have been using the following code:</p>
  
  <pre><code>#Load example shapefile
  shp &lt;- readShapeSpatial("species1.shp")
  
  #How many polygon slots are there?
  length(shp@polygons)
  
  &gt;2
  
  #How many polygons are in each slot
  length(shp@polygons[[1]]@Polygons
  length(shp@polygons[[2]]@Polygons
  </code></pre>
  
  <p>and to get the area of a particular one:</p>
  
  <pre><code>shp@polygons[[1]]@Polygons[[1]]@area
  </code></pre>
  
  <p>Is this correct? I'm worried that a lake in the middle of the range might constitute a polygon on its own? I want to end up with a list that is roughly like:</p>
  
  <pre><code>           Species A    Species B
  Polygon 1      12          11
  Polygon 2      13          10
  Polygon 2      14          NA
  </code></pre>
  
  <p>If I wanted to compile a list for every species of how many polygons and their individual ranges would be pretty straightforward to pass to a loop if the above code is correct. </p>
  
  <p>Thanks</p>