title: Create a boolean column based on data in another column in R
body: <p>I have a data set and would like to do two things:</p>
  
  <ol>
  <li><p>Set certain row values in Col A to 0 based on values in Col B</p></li>
  <li><p>Create a new column with values of either 0 or 1 based on the edited values in Col A</p></li>
  </ol>
  
  <p>My current approach is shown below - the issue is I occasionally get an error: </p>
  
  <pre><code>Error in `[&lt;-.data.frame`(`*tmp*`, "OCS_dose", value = 0) : 
    replacement has 1 row, data has 0
  </code></pre>
  
  <p>As the numbers that I am generating are randomly selected and on certain trials there are no rows to update in Col A based on the numbers in Col B. </p>
  
  <p>Here is an example of my code that causes the error:</p>
  
  <pre><code>pbo_IFNlow_data[pbo_IFNlow_data$OCS_status == 0,]['OCS_dose'] &lt;- 0
  </code></pre>
  
  <p>OCS_status is either a 0 or 1 that is generated using:</p>
  
  <pre><code>pbo_OCS_status_low &lt;- sample(c(0,1), replace = TRUE,
                               size = pbo_n_IFNlow, prob=c(1-.863, 0.863))
  </code></pre>
  
  <p>Therefore on occasion, I have no 0's... In my mind R should then just not try to update anything.</p>
  
  <p>Is there a better way to do what I am trying to do?</p>
  
  <p>Here is a more complete segment of my code:</p>
  
  <pre><code>  pbo_OCS_status_low &lt;- sample(c(0,1), replace = TRUE, size = pbo_n_IFNlow, prob=c(1-.863, 0.863)) #on OCS = 1
  
    #OCS dose
    pbo_OCS_dose_low &lt;- rtruncnorm(pbo_n_IFNlow, a=0, b=Inf, mean=12.8, sd=8.1)
  
    #IFN boolean flag
    pbo_IFN_low &lt;- rep(0, pbo_n_IFNlow)
  
    #SLEDAI score 
    pbo_SLEDAI_low &lt;- rtruncnorm(pbo_n_IFNlow, a=0, b=Inf, mean=11.1, sd=4.4)
  
    #Response criteria met for SRI score reduction
    pbo_SRI_low &lt;- sample(c(0,1), replace = TRUE, size = pbo_n_IFNlow, prob=c(1-0.423, 0.423))
  
  
    pbo_IFNlow_data &lt;- cbind(IFN_status=pbo_IFN_low,
                             OCS_status=pbo_OCS_status_low,
                             OCS_dose=pbo_OCS_dose_low,
                             SLEDAI=pbo_SLEDAI_low,
                             SRI_response=pbo_SRI_low)
  
    pbo_IFNlow_data &lt;- data.frame(pbo_IFNlow_data)
  
    #set those off OCS to 0
    pbo_IFNlow_data[pbo_IFNlow_data$OCS_status == 0,]['OCS_dose'] &lt;- 0
  
  
    #stratifcation factor for OCS dosage
    pbo_IFNlow_data$OCS_lessthan10 &lt;- "temp"
    pbo_IFNlow_data[pbo_IFNlow_data$OCS_dose &lt; 10, ]['OCS_lessthan10'] &lt;- 1
    pbo_IFNlow_data[pbo_IFNlow_data$OCS_dose &gt;= 10, ]['OCS_lessthan10'] &lt;- 0
  
  
    #stratification factor for SLE score
    pbo_IFNlow_data$SLE_lessthan10 &lt;- "temp"
    pbo_IFNlow_data[pbo_IFNlow_data$SLEDAI &lt; 10, ]['SLE_lessthan10'] &lt;- 1
    pbo_IFNlow_data[pbo_IFNlow_data$SLEDAI &gt;= 10, ]['SLE_lessthan10'] &lt;- 0
  </code></pre>