title: r - for loop to compare 2 dataframes all by all rows
body: <p>I'm trying to compare 2 columns from dataframe <code>d1</code> with 2 columns from dataframe <code>d2</code>, row by row. To illustrate the issue I created dummy datasets:</p>
  <pre><code>d1 &lt;- data.frame(
    a = c(1,2,3),
    b = c(4,5,6)
  )
  
  d2 &lt;- data.frame(
    a = c(2,0,2),
    b = c(5,5,6)
  )
  </code></pre>
  <p>Ideally, I would like to flag all rows in <code>d1</code> for which I can find a match in at least one row of <code>d2</code>, so the wanted result would be:</p>
  <pre><code>data.frame(
    a = c(1,2,3),
    b = c(4,5,6),
    flag = c(0,1,0)
  )
  </code></pre>
  <p>this is what I tried:</p>
  <pre><code>for (i in 1:nrow(d1)) {
    for (j in 1:nrow(d2)) {
      test[i,j] = ifelse(d1$a[i] == d2$a[j] &amp; d1$b[i] == d2$b[j], 1, 0)
    }
  }
  </code></pre>
  <p>a for loop would be the best solution</p>