title: R if else with for loop
body: <p>I would like to calculate a ratio (A / B = C). Then I want to assign a label for ratio. If the ratio equals 0 label gets "0". If value less than 1 label gets "1". Lastly ratio is greater than 1 label gets "2".</p>
  
  <p>Here is the example data:</p>
  
  <pre><code>                    A            B         C
    1                 0        171.06  0.0000000
    2                 1             2  0.5
    3                 0        120.84  360.00000
    4                 0        308.07  0.0000000
    5                 0        243.06  0.0000000
    6                 0       876.015  0.0000000
  </code></pre>
  
  <p>Here is the function with loop:</p>
  
  <pre><code>targets &lt;- function(data)
  {
    label_df &lt;- data[,c("A", "B")]
      label_df[is.na(label_df)] &lt;- 0
      label_df["C"] &lt;- (label_df["A"] / label_df["B"]) 
      for(i in 1:nrow(label_df))
      {
        if(label_df$C == 0){label_df[i,"C"] &lt;- 0}
  
         else if(label_df$C &lt; 1){label_df[i,"C"] &lt;- 1}
         else {label_df[i,"C"] &lt;- 2}
        }
  return(as.data.frame(label_df))
      }
  </code></pre>
  
  <p>And here is the result. It shows all labels 0.  </p>
  
  <pre><code>lab &lt;- target(data)      
  head(lab)      
  
                      A            B         C      label     
    1                 0        171.06  0.0000000    0
    2                 1             2  0.5          0
    3                 0        120.84  360.00000    0
    4                 0        308.07  0.0000000    0
    5                 0        243.06  0.0000000    0
    6                 0       876.015  0.0000000    0
  </code></pre>
  
  <p>I check the result with table() function and it just shows all data for label 0. There are no 1 or 2 labels. I couldn't figure out where the problem is. Any idea?</p>