title: Compare rows of a data frame with a matrix rows in R
body: <p>I have created a matrix like this:</p>
  
  <pre><code>&gt; head(matrix)
       Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9 Var10 Var11
  [1,] "0"  "0"  "1"  "0"  "1"  "1"  "0"  "0"  "0"  "0"   "NA"  
  [2,] "1"  "0"  "1"  "0"  "1"  "1"  "0"  "0"  "0"  "0"   "NA"  
  [3,] "0"  "1"  "1"  "0"  "1"  "1"  "0"  "0"  "0"  "0"   "NA"  
  [4,] "1"  "1"  "1"  "0"  "1"  "1"  "0"  "0"  "0"  "0"   "NA"  
  [5,] "0"  "0"  "2"  "0"  "1"  "1"  "0"  "0"  "0"  "0"   "NA"  
  [6,] "1"  "0"  "2"  "0"  "1"  "1"  "0"  "0"  "0"  "0"   "NA"
  </code></pre>
  
  <p>Now, I want to compare the matrix above with the following data frame:</p>
  
  <pre><code>&gt; head(df)
         cod Var11 Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9 Var10     Var12
  1  C000354     B    1    1    4    0    1    2    0    0    0     1  51520.72
  2  C000404     A    1    0    1    0    4    4    0    0    1     1  21183.25
  3  C000444     A    1    0    4    1    3    3    0    0    0     1  67504.74
  4  C000480     A    1    1    2    0    2    3    0    0    1     1  26545.92
  5  C000983     C    1    0    1    0    3    4    0    0    0     0  10379.37
  6  C000985     C    1    0    3    1    3    4    0    0    0     0  18660.99
  </code></pre>
  
  <p>Matrix contains all possible combinations of the variables <code>Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9 Var10</code>, so basically when a row of <code>df</code> (only column from <code>VAR1</code> to <code>VAR10</code>) match with a row of <code>matrix</code> and this row in <code>df</code> had a <code>Var12&gt;=90000</code>, I would like it to be written <code>"A"</code> in corresponding column <code>VAR11</code> of <code>matrix</code>.</p>
  
  <p>I have tried with this:</p>
  
  <pre><code>for (i in 1 : nrow(matrix)) {
    for (j in 1 : 10) {
      ifelse(matrix[i,j]==df[,(j+2)]
             &amp;&amp; df$Var12[] &gt;= 90000,
             matrix[i,"Var11"] &lt;- "A",
             matrix[i,"Var11"] &lt;- "NA")
    }
  }
  </code></pre>
  
  <p>But this writes <code>NA</code> in all rows of matrix.</p>
  
  <p>Does anyone know why this happen or how to solve it?</p>
  
  <p>Thanks in advance.</p>