title: add a column to my dataframe based on information in two columns
body: <p>I want to add a column to my dataframe based on information in two columns of the dataframe.</p>
  <p>In my example dataframe, I have two lines of entries for one sample, which are  rows 3 &amp; 4. I want to code that writes a new column &quot;main&quot; and fills in &quot;1&quot; each row that has a unique tag number. For rows with duplicate tags numbers, I need the row with the highest weight to be &quot;1&quot; in main, and all other rows to be filled with &quot;0&quot;.</p>
  <pre><code>df
         sp    weight   tag
  1   green        70     1
  2  yellow        63     2
  3     red        41     3
  4     red        25     3
  5     red         9     3
  </code></pre>
  <pre><code>df with &quot;main&quot; column added
         sp    weight   tag  main
  1   green        70     1     1
  2  yellow        63     2     1
  3     red        41     3     1
  4     red        25     3     0
  5     red         9     3     0
  </code></pre>
  <p>Here's what I have so far:</p>
  <pre><code>df$is.uniq &lt;- duplicated(df$tag) | duplicated(df$tag), fromLast = TRUE) 
  df$main &lt;- ifelse(is.uniq==TRUE, &quot;1&quot;, ifelse(is.uniq==FALSE, &quot;0&quot;, NA  )) 
  </code></pre>
  <p>I know i need to change the second ifelse statement to reference the weight column and fill 1 for the greatest weight and 0 for all else, but I haven't figured out how to do that yet.</p>