title: How to skip an error in a loop
body: <p>I want to skip an error (if there is any) in a loop and continue the next iteration. I want to compute 100 inverse matrices of a 2 by 2 matrix with elements randomly sampled from {0, 1, 2}. It is possible to have a singular matrix (for example, </p>
  
  <pre><code>1 0
  2 0
  </code></pre>
  
  <p>Here is my code</p>
  
  <pre><code>set.seed(1)
  count &lt;- 1
  inverses &lt;- vector(mode = "list", 100)
  repeat {
      x &lt;- matrix(sample(0:2, 4, replace = T), 2, 2)
      inverses[[count]] &lt;- solve(x)
      count &lt;- count + 1
      if (count &gt; 100) break
  }
  </code></pre>
  
  <p>At the third iteration, the matrix is singular and the code stops running with an error message. In practice, I would like to bypass this error and continue to the next loop. I know I need to use a <code>try</code> or <code>tryCatch</code> function but I don't know how to use them. Similar questions have been asked here, but they are all really complicated and the answers are far beyond my understanding. If someone can give me a complete code specifically for this question, I really appreciate it.</p>