title: R regex - replacing nth match
body: <p>Suppose you want to replace the second string match with the second element in the vector replacement. For instance:</p>
  
  <pre><code>x &lt;- "CBCADEFGHI"
  pattern &lt;- "(A|D|C)"
  replacement &lt;- c("X","Y","Z")
  </code></pre>
  
  <p>How would you go about replacing only the second pattern match, which is "C" because its the second pattern that is discovered, with the corresponding replacement vector element "Z"? </p>
  
  <p>Desired Output:</p>
  
  <pre><code>"CBZADEFGHI"
  </code></pre>