title: Replace the first N dots of a string revisited
body: <p>In January I asked how to replace the first N dots of a string: <a href="https://stackoverflow.com/questions/14573274/replace-the-first-n-dots-of-a-string">replace the first N dots of a string</a></p>
  
  <p>DWin's answer was very helpful.  Can it be generalized?</p>
  
  <pre><code>df.1 &lt;- read.table(text = '
  
           my.string     other.stuff
  
       1111111111111111     120
       ..............11     220
       11..............     320
       1...............     320
       .......1........     420
       ................     820
       11111111111111.1     120
  
  ', header = TRUE)
  
  nn &lt;- 14
  
  # this works:
  
  df.1$my.string &lt;- sub("^\\.{14}", paste(as.character(rep(0, nn)), collapse = ""), 
  df.1$my.string)
  
  # this does not work:
  
  df.1$my.string &lt;- sub("^\\.{nn}", paste(as.character(rep(0, nn)), collapse = ""), 
  df.1$my.string)
  </code></pre>