title: How do I delete the last row of a group in r based on conditions in a different row in r
body: <p>I have a data frame like this (NT and MCS are text placeholders for a certain species let's say)</p>
  
  <pre><code>A &lt;- c("NT", "MCS","MCS","NT", "MCS", "MCS", "NT", "MCS", "MCS", "MCS", 
         "NT", "MCS", "MCS","NT", "MCS","NT","NT","MCS", "MCS","NT")
  B &lt;- c("1", "3", "3","3","3", "3","3","4","4","4","4","3", 
         "3","3","2","2","1","3","3","3")
  C &lt;- c("G1", "G2", "G2", "G2", "G3", "G3", "G3", "G4", "G4", "G4", "G4", 
         "G5", "G5", "G5","G6", "G6", "G7","G8","G8","G8")
  
  df &lt;- data.frame(A,B,C)
  
  A   B   C
  NT  1   G1
  MCS 3   G2
  MCS 3   G2
  NT  3   G2
  MCS 3   G3
  MCS 3   G3
  NT  3   G3
  MCS 4   G4
  MCS 4   G4
  MCS 4   G4
  NT  4   G4
  MCS 3   G5
  MCS 3   G5
  NT  3   G5
  MCS 2   G6
  NT  2   G6
  NT  1   G7
  MCS 3   G8
  MCS 3   G8
  NT  3   G8
  </code></pre>
  
  <p>The A column represents the species. The B column represents an integer value that equals the the number of rows in each group. C column represents unique groups. The criteria is as follows: Delete the LAST row of every group/species IF B > 1. If B = 1, then NT (or the only row in that group) should remain. Here is what I need it to look like.  </p>
  
  <pre><code>A   B   C
  NT  1   G1
  MCS 3   G2
  MCS 3   G2
  MCS 3   G3
  MCS 3   G3
  MCS 4   G4
  MCS 4   G4
  MCS 4   G4
  MCS 3   G5
  MCS 3   G5
  MCS 2   G6
  NT  1   G7
  MCS 3   G8
  MCS 3   G8
  
  new&lt;- df %&gt;% group_by(A, B) %&gt;% slice(if(any(numb &gt; 1)) 1:n())
  </code></pre>
  
  <p>The above is the closest code I've run but it doesn't evaluate to an integer or numeric vector (which is what I need it to do). I did it like this:</p>
  
  <pre><code>new &lt;- df %&gt;% group_by(A, B) %&gt;% 
                slice(if(any(B &gt; 1)) 1 else 1:n())
  </code></pre>
  
  <p>but it got rid of the repeated values (so all the ones in column A I did not want to delete - not the last row regardless of repetition). Is there something I'm missing in the code I've run or another method that would accomplish this (ideally in dplyr but I'd be curious about all methods)?</p>