title: Delete matrix rows based on a threshold coverage with another matrix in r
body: <p>I have a <code>matrix</code> composed of sites and species. Some species have a certain trait value but not all of them.
  I want to keep only the site-species <code>matrix</code> rows that contain enough trait information, in my case more than 60%.</p>
  
  <p>So far, I have the following <code>for-loop</code> but I would like to have a faster version of this code. How can I optimize this and skip the <code>for-loop</code> part?</p>
  
  <pre><code># site-species matrix
  A &lt;- matrix(c(0, 0.2, 0.2, 0.6, 0.3, 0.3, 0, 0.4), byrow = T, nrow = 2)
  colnames(A) &lt;- paste0("sp_", seq(ncol(A)))
  rownames(A) &lt;- paste0("site_", seq(nrow(A)))
  # trait information
  B &lt;- data.frame(sp = paste0("sp_", seq(1:ncol(A))),
                  value = c(NA, NA, 2, 3))
  # For-loop to get the coverage percentage for each row
  pcover &lt;- c()
  for(i in 1:nrow(A)){
    non_null_A &lt;- A[i, ][A[i, ] &gt; 0]
    B_match &lt;- match(names(non_null_A), B[, "sp"])
    B_value &lt;- B[B_match, "value"]
    pcover &lt;- rbind(pcover,
                    sum(!is.na(B_value)) / length(B_value) * 100)
  }
  A
  A[pcover &gt; 60, , drop = FALSE] # in this case, the second site is removed
  </code></pre>