title: Ungroup some but not all variables in R + dplyr
body: <p>In <code>dplyr</code> with R, if I've grouped by multiple variables can I then ungroup by a subset of those with <code>ungroup</code>? As far as I can tell, <code>ungroup</code> ignores any arguments provided and always ungroups everything.</p>
  
  <p>Example:</p>
  
  <pre><code>mtcars %&gt;%
    group_by(cyl, vs, am) %&gt;%
    # this removes 'am' but grouping by 'cyl' and 'vs' remain
    summarize(n = n()) %&gt;%  
    ungroup(vs)  # I want this to work but it ungroups everything, not just 'vs'
  </code></pre>
  
  <p>Is there a way to remove the grouping by <code>vs</code> while keeping the grouping by <code>cyl</code>? I can accomplish this via</p>
  
  <pre><code>mtcars %&gt;%
    group_by(cyl, vs, am) %&gt;%
    summarize(n = n()) %&gt;%  
    ungroup() %&gt;%
    group_by(cyl)
  </code></pre>
  
  <p>but I feel like there has to be a better way than removing both groupings and re-adding just the <code>cyl</code> grouping. </p>
  
  <p>I'm using the <code>summarize</code> in my examples because that's my particular context, but this behavior seems to be evident even in something as minimal as this:</p>
  
  <pre><code>mtcars %&gt;%
    group_by(cyl, vs) %&gt;%
    ungroup(vs)  # ungroups everything, not just 'vs'
  </code></pre>
  
  <hr>
  
  <p><strong>Update</strong>: Thanks everyone for the great suggestions and comments. I just tried this and it works with no fuss due to <code>add=FALSE</code> in <code>group_by</code>, and it's as clean as what I was hoping <code>ungroup</code> could do:</p>
  
  <pre><code>mtcars %&gt;%
    group_by(cyl, vs, am) %&gt;%
    summarize(n = n()) %&gt;%  
    group_by(cyl)
  </code></pre>
  
  <p>Thanks to @camille and @julius-vainora I know that <code>ungroup</code> by itself cannot do this, so I'll have to do grouping rather than ungrouping. @RyanD's suggestions were also very helpful. </p>