title: Fastest way of using conditions in for loop
body: <p>So for the dataset <em>birthwt</em> I want the percentage of low weight babies that are born from mothers that smoke and were under 20 years when given birth. In other words, I want how many &lt;2600 of bwt (weight) are for age &lt;20 and smoke == 1. </p>
  
  <p>I run the next three chunks of code that actually gives you the correct answer:</p>
  
  <pre><code> # new df with the conditions
  new_df &lt;- subset(birthwt, age&lt;20 &amp; smoke==1)
  
   #for loop to calculate the low weight
  
  low_weight &lt;- 0
  for (i in 1:length(new_df$bwt)){
    if(bwt[i] &lt; 2600){
      low_weight &lt;- low_weight + 1
    }
  }
  
   #low weight for the original dataset
  low_weight_tot &lt;- 0
  attach(birthwt)
  for (i in 1:length(birthwt$bwt)){
    if(bwt[i] &lt; 2600){
      low_weight_tot &lt;- low_weight_tot + 1
    }
  }
  
  print(low_weight/low_weight_tot)*100
  </code></pre>
  
  <p>But I find it very tedious, is there any other simpler way of doing this with loops?</p>
  
  <p>Thanks!</p>