title: R: how to remove duplicate rows by column
body: <pre><code>df &lt;- data.frame(id = c(1, 1, 1, 2, 2),
                   gender = c("Female", "Female", "Male", "Female", "Male"),
                   variant = c("a", "b", "c", "d", "e"))
  &gt; df
    id gender variant
  1  1 Female       a
  2  1 Female       b
  3  1   Male       c
  4  2 Female       d
  5  2   Male       e
  </code></pre>
  
  <p>I want to remove duplicate rows in my data.frame according to the <code>gender</code> column in my data set. I know there has been a similar question asked (<a href="https://stackoverflow.com/questions/24011246/deleting-rows-that-are-duplicated-in-one-column-based-on-the-conditions-of-anoth">here</a>) but the difference here is that I would like to remove duplicate rows within each subset of the data set, where each subset is defined by an unique <code>id</code>. </p>
  
  <p>My desired result is this:</p>
  
  <pre><code>  id gender variant
  1  1 Female       a
  3  1   Male       c
  4  2 Female       d
  5  2   Male       e
  </code></pre>
  
  <p>I've tried the following and it works, but I'm wondering if there's a cleaner, more efficient way of doing this?</p>
  
  <pre><code>out = list()
  for(i in 1:2){
    df2 &lt;- subset(df, id == i)
    out[[i]] &lt;- df2[!duplicated(df2$gender), ]
  }
  do.call(rbind.data.frame, out)
  </code></pre>