title: R Removing duplicate entries in dataframe and keeping rows with fewer NAs and zeroes
body: <p>I would like to deduplicate a data.frame I am generating from another
  part of my codebase without the ability to know the order of the
  columns and rows. The data.frame has some columns I want to compare
  for duplication, here <code>A</code> and <code>B</code>, but I would like to then choose the
  ones to keep from the rows that contain fewer NAs and zeros in other
  columns in the dataframe, here <code>C</code>, <code>D</code> and <code>E</code>.</p>
  
  <pre><code>tc=
   'Id  B   A   C  D  E
     1  62  12  0  NA  NA
     2  12  62  1  1  1                  
     3  2   62  1  1  1
     4  62  12  1  1  1
     5  55  23  0  0  0      '
  
  df =read.table(textConnection(tc),header=T)
  </code></pre>
  
  <p>I can use <code>duplicated</code>, but since I cannot control the order of the
  columns and rows where my dataframe comes, I need a way to get the
  unique values with fewer NAs and zeros.</p>
  
  <p>This will work in the example, but won't if the incoming data.frame
  has a different order:</p>
  
  <pre><code>df[!duplicated(data.frame(A=df$A,B=df$B),fromLast=TRUE),]
    Id  B  A C D E
  2  2 12 62 1 1 1
  3  3  2 62 1 1 1
  4  4 62 12 1 1 1
  5  5 55 23 0 0 0
  </code></pre>
  
  <p>Any ideas?</p>