title: sub() function replaces all matches
body: <p>it's me again,</p>
  
  <p>I have a problem with the sub()-function in R:
  The documentation says: "sub and gsub perform replacement of the first and all matches respectively."</p>
  
  <p>I have a code, where I want to replace only one item with another (conditionally, say that if an item appears I want it to be two times in a vector - but that's not necessary information here).</p>
  
  <p>However, if I perform the sub-function, it replaces everything. Here some code to demonstrate what's happening:</p>
  
  <pre><code>a = c("a", "b", "c", "d", "a", "b", "c", "d", "a", "a")
  &gt; sub("a", "f", a)
  [1] "f" "b" "c" "d" "f" "b" "c" "d" "f" "f"
  &gt; gsub("a", "f", a)
  [1] "f" "b" "c" "d" "f" "b" "c" "d" "f" "f"
  </code></pre>
  
  <p>As you can see, in my case sub and gsub perform the same action, however I want the result to be the following:</p>
  
  <pre><code>a = c("a", "b", "c", "d", "a", "b", "c", "d", "a", "a")
  &gt; sub("a", "f", a)
  [1] "f" "b" "c" "d" "a" "b" "c" "d" "a" "a"
  </code></pre>
  
  <p>I don't understand why that is happening, to me it does not seem in line with the documentation. Can anybody explain to me the reason or a workaround?</p>