title: create a new column based on group in existing column in R
body: <p>I am cleaning some data in R and have a dataset like this:</p>
  
  <pre><code>x1, x2, x3
  1, 24, 41
  1, 22, 40
  1, 21, 38
  2, 20, 40
  2, 21, 40
  3, 22, 41
  3, 24, 40
  4, 20, 41
  </code></pre>
  
  <p>I want to add a new column, and the value of each row is based on both x1 and x2 column. Within each group in x1, I want to know if the value in x2 is greater than or equal to, say 24. If true, all the values in the new column for that group are set to 1.</p>
  
  <p>So data should look like this:</p>
  
  <pre><code>x1, x2, x3, x4
  1, 24, 41, 1
  1, 22, 40, 1
  1, 21, 38, 1
  2, 20, 40, 0
  2, 21, 40, 0
  3, 22, 41, 1
  3, 24, 40, 1
  4, 20, 41, 0
  </code></pre>
  
  <p>The purpose of this is for aggregating the rows. I would like to aggregate the data based on groups in x1, but still need information on the other columns. </p>