title: check if a value exists group by id in R
body: <p>I want to define a variable new_var by the following criteria:</p>
  
  <p>For each id, if exist reference=1, then new_var=1; else new_var=0, for exemple:</p>
  
  <pre><code>id      reference 
  A            0   
  A            1     
  B            0   
  B            0  
  </code></pre>
  
  <p>Create a  <code>new_var</code>, the table looks like this:</p>
  
  <pre><code>id      reference     new_var
  A            0          1
  A            1          1
  B            0          0
  B            0          0
  </code></pre>
  
  <p>I only know that I can use the statement <code>case when</code> in SAS, but in R how to do this?</p>