title: R: Summation of Single Column Based on Conditions
body: <p>I have a dataset that has (with the help of this site) created a count based on 3 instances:</p>
  
  <ol>
  <li>Number of <code>Plans</code> where <code>Coins</code> is greater than 20</li>
  <li>Number of <code>Plans</code> where <code>Coins</code> is equal to 20</li>
  <li>Number of <code>Plans</code> where <code>Coins</code> is less than 20</li>
  </ol>
  
  <p>Here's a sample from the much larger dataset</p>
  
  <pre><code>Plan    Year   Coins   Copay   Enrollment
    A     2018     20      10       200
    B     2014     15       5       100
    C     2012     30       0        50
    D     2017     30      10       350
    E     2018     5       10       400
    F     2018     20       0       150
    G     2018     20       0       200
    H     2016     20      10       800
    I     2014     10       3       250
    J     2017     20       7       550
  </code></pre>
  
  <p>This was achieved through:</p>
  
  <pre><code>df %&gt;%
     group_by(grp = case_when(Coins &lt; 20 ~ 'grp1', Coins ==20 ~ 'grp2', TRUE ~ 'grp3')) %&gt;%
     summarise(Plan = toString(unique(Plan)), prop = n()) %&gt;%
     ungroup %&gt;%
     select(-grp)
  </code></pre>
  
  <p>To get:</p>
  
  <ol>
  <li>C, D - 2    </li>
  <li>A, F, G, H, J - 5</li>
  <li>B, E, I - 3</li>
  </ol>
  
  <p>I would now like to involve the <code>Enrollment</code> column and sum based on the three conditions above. Thus, I would like to achieve the following counts:</p>
  
  <ol>
  <li>400 (50+350)</li>
  <li>1900 (200+150+200+800+550)</li>
  <li>750 (100+400+250)</li>
  </ol>