title: Hot to get clean polygons in R after st_combine/st_union with sf package?
body: <p>I have a tidy dataset for census sectors in sf format (<a href="https://github.com/bruno-pinheiro/app_acesso_saude/raw/master/data/setores_sp_ok.rda" rel="nofollow noreferrer">setores_sp_ok.rda</a>), which has polygons for two different territorial models, indicated by variable <code>modelo</code>. I want to aggregate census sectors by <code>modelo</code> and <code>cnes</code>, to create another dataset with new boundaries.</p>
  
  <p>I can do this using <code>group_by()</code> + <code>summarise()</code> technique, which automatically uses <code>st_union()</code> to aggregate polygons. But the result is poor, with many internal boundaries.</p>
  
  <pre><code># load packages
  library(dplyr)
  library(ggplot2)
  library(sf)
  library(lwgeom)
  
  # import data
  load(url("https://github.com/bruno-pinheiro/app_acesso_saude/raw/master/data/setores_sp_ok.rda"))
  
  # combine polygons
  ubs_malhas &lt;- setores_sp %&gt;% 
    st_make_valid() %&gt;% 
    group_by(cnes, modelo) %&gt;% 
    summarise(area = sum(area)) %&gt;% 
    ungroup()
  
  # plot
  ggplot(ubs_malhas[ubs_malhas$modelo == "vigente", ]) +
    geom_sf(lwd = .2)
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/rSPdC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rSPdC.png" alt="result have many internal boundaries"></a></p>
  
  <p>I know that is possible to realize this kind of operation combining <code>st_combine</code>, <code>st_union</code> and <code>st_intersect</code>, but I'm not realizing how to make it.</p>
  
  <p>How to combine polygons by <code>modelo</code> and <code>cnes</code> and get clean aggregated polygons, without internal boundaries?</p>
  
  <p>Someone has any tip?</p>
  
  <p>Many thanks!</p>