title: How to add a binary variable to a data frame based on another variable in the data frame in r?
body: <p>My data (train) is a 443402 x 27 data frame and I have initialized a new binary variable train$researchedplan, to "1". There are 64,673 unique train$customer_ID's (each customer is entered a random amount of times in the data frame - but in order. i.e. the first customer has the first 9 rows, the second customer has the next 6, etc.).</p>
  
  <pre><code>&gt; train[1:20,c(1,27)]
  &gt; customer_ID researchedplan
  1     10000000              1
  2     10000000              1
  3     10000000              1
  4     10000000              1
  5     10000000              1
  6     10000000              1
  7     10000000              1
  8     10000000              1
  9     10000000              1
  10    10000005              1
  11    10000005              1
  12    10000005              1
  13    10000005              1
  14    10000005              1
  15    10000005              1
  24    10000013              1
  25    10000013              1
  26    10000013              1
  27    10000013              1
  28    10000014              1
  </code></pre>
  
  <p>I also have a vector (diff_than_researched) that comprises of a string of some unique train$customer_ID's, identifying which customers did not research a particular plan. 
  For the strings in diff_than_researched that match the strings in train$customer_ID, I would like train$researchedplan for all entries for the customer to be "0". 
  e.g.:</p>
  
  <pre><code>&gt; head(diff_than_researched)
  &gt;[1] "10000019" "10000033" "10000036" "10000037" "10000055" "10000075"
  </code></pre>
  
  <p>so, for all of the "10000019" entries, I would like train$researchedplan to equal "0".</p>
  
  <p>Now, I can do this all with a "for loop", but it takes too long to loop through so many entries:</p>
  
  <blockquote>
    <p>for(i in 1:17210) { 
      train$researchedplan[train$customer_ID == diff_than_researched[i]] &lt;- 0
    }</p>
  </blockquote>