title: In R, how do I classify each row of a data frame based on the bin its values fall into?
body: <p>In R, I want to classify each rows of the data frame by binning the values and using the number (sum) of values in each bin to assign them into 2 groups (classes) by using if-else logic.</p>
  
  <ul>
  <li>Within an R for-loop, I used the R cut and split commands to bin the
  values by row.</li>
  <li>The bins (ranges) are: 1..9, 10..19, 20..29, 30..39, 40..49.</li>
  <li>If a row contains 1 pair of values falling in the same bin (range),
  say 10..19, then it should be classified as "P". If it contains 2
  pairs falling into 2 different bins (ranges), then they should be
  classified as "PP".</li>
  <li>Then I created 2 new variables named p and pp by using hard-coded
  conditions/rules. The values in the variables are either TRUE or
  FALSE, depending whether the n-th row meet the those rules.</li>
  <li>Finally, I used p and pp as conditions in the if-else statement to
  assign each row to either class P (1st row), or class PP (2nd row).</li>
  </ul>
  
  <p>First, I created a data frame x:</p>
  
  <pre><code>n1 &lt;- c(1, 7); n2 &lt;- c(2, 11); n3 &lt;- c(10, 14); n4 &lt;- c(23, 32); n5 &lt;- c(37, 37); n6 &lt;- c(45, 41)
  x &lt;- data.frame(n1, n2, n3, n4, n5, n6)
  x
    n1 n2 n3 n4 n5 n6
  1  1  2 10 23 37 45
  2  7 11 14 32 37 41
  </code></pre>
  
  <p>The 1st row should be classified as "P", because it has 1 pair of values (1, 2) falling in the same bin 1..10.<br>
  The 2nd row should be classified as "PP", because it has 2 pairs of values (11, 14 and 32, 37) falling in 2 bins: 10..19 and 30..39, accordingly.    </p>
  
  <p>So, after creating the data frame x, I created a for-loop:</p>
  
  <pre><code>for(i in nrow(x)){
  
  # binning the data:
    bins &lt;- split(as.numeric(x[i, ]), cut(as.numeric(x[i, ]), c(0, 9, 19, 29, 39, 49)))
    # creating the rule for p (1 pair of numbers falling in the same range)
    p &lt;- (sum(lengths(bins) == 2) == 1 &amp; sum(lengths(bins) == 1) == 4)
    # creating the rule for pp (2 different pairs, each has 2 numbers falling in the same range)
    pp &lt;- (sum(lengths(bins) == 2) == 2 &amp; sum(lengths(bins) == 1) == 2 &amp; sum(lengths(bins) == 0) == 1)
  
    if(p){
      x$types &lt;- "P"
    } else if(pp){
      x$types &lt;- "PP"
    } else{
      stop("error")
    }
    }
  
  print(x)
  </code></pre>
  
  <p>I want to create a new column named types, holding the class P or PP:</p>
  
  <pre><code>  n1 n2 n3 n4 n5 n6 types
  1  1  2 10 23 37 45 P
  2  7 11 14 32 37 41 PP
  </code></pre>
  
  <p>Instead the code returned only PP:</p>
  
  <pre><code>  n1 n2 n3 n4 n5 n6 types
  1  1  2 10 23 37 45 PP
  2  7 11 14 32 37 41 PP
  </code></pre>
  
  <p>This is because the loop runs twice over the rows. But if it runs only once, all the rows are classified as "P", instead of "PP". I expect it's something very simple, just was not able to figure it out so far.</p>