title: Summing values of attributes of overlapping polygons in SpatialPolygonsDataFrame with R
body: <p>I have one shape file containing lots of SpatialPolygons which are partly overlapping. These polygons belong to the application of a fungicide on a field and each polygon has an associated application rate as attribute. </p>
  
  <p>What I want to obtain is to correct AsApplied map taking into account the overlapping areas meaning that the rate should be summed up and merged if two (or more) polygons are overlapping. </p>
  
  <p>The following example code creates a SpatialPolygonsDataFrame simplifying the problem: </p>
  
  <pre><code>library(raster)
  library(sp)
  
  p&lt;-SpatialPolygons(list(Polygons(list(Polygon(cbind(c(1,4,4,3,3,1,1),c(1,1,3,3,4,4,1)),hole = F)), "1_ "),
                      Polygons(list(Polygon(cbind(c(3,4,4,3,3),c(3,3,4,4,3)),hole = F)), "1_2"),
                      Polygons(list(Polygon(cbind(c(3,4,4,3,3),c(3,3,4,4,3)),hole = F)), "2_1"),
                      Polygons(list(Polygon(cbind(c(4,4,5,5,3,3,4),c(4,3,3,5,5,4,4)),hole = F)),"2_")))
  
  pid &lt;- sapply(slot(p, "polygons"), function(x) slot(x, "ID"))
  p.df &lt;- data.frame( ID=1:length(p), row.names = pid) 
  p &lt;- SpatialPolygonsDataFrame(p, p.df)
  p$Rate &lt;- c(100, 100, 100, 100)
  crs(p) &lt;- "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"
  plot(p)
  </code></pre>
  
  <p>You can see two squares from four polygons which are partly overlapping. Each polygon has an associated rate of 100.
  What I would like to have is three polygons. The two non overlapping ones should have the rate 100 and the two overlapping ones should be joined to one polygon having a value of 200. </p>
  
  <p>I already tried the union or intersect functions of the raster package but was only able to get the information which polygons overlap but not the summing and merging. In addition I am seeking explicitly for a solution in R. </p>
  
  <p>Any help solving this problem is highly appreciated. </p>
  
  <p><strong>Update:</strong> The solution provided by RobertH provided below works for my simple example. Thank you very much already!</p>
  
  <p>However when switching to my real usecase I am getting to following kind of errors and warnings: </p>
  
  <pre><code>Error in if (is.numeric(i) &amp;&amp; i &lt; 0) { : 
    missing value where TRUE/FALSE needed
  In addition: Warning messages:
  1: In RGEOSUnaryPredFunc(spgeom, byid, "rgeos_isvalid") :
    Too few points in geometry component at or near point 8.3634020800000002 50.056772690000003
  ...
  </code></pre>
  
  <p>An example shape file is uploaded here: (obsolete)</p>
  
  <p>Any ideas how to deal with this problem? </p>
  
  <p><strong>Update #2</strong> Using the current development version 2.5-10 indeed fixes the warnings in RGEOSUnaryPredFunc. However, if polygons are only overlapping very very little I am still getting the error: </p>
  
  <pre><code>Error in if (is.numeric(i) &amp;&amp; i &lt; 0) { : 
    missing value where TRUE/FALSE needed
  </code></pre>
  
  <p>An example shape file for which this is happening is uploaded here: <a href="http://www.share-online.biz/dl/O4ZIVH8OBW" rel="nofollow">http://www.share-online.biz/dl/O4ZIVH8OBW</a>. More precise the field looks like the following: </p>
  
  <p><a href="http://i.stack.imgur.com/GEgl8.png" rel="nofollow">Image of polygon example 2</a></p>
  
  <p>The two polygons marked in red cause the error and if one of the two is removed the union works fine. </p>
  
  <p>Thank you very much already for your great help!</p>