title: retrieve fixed length subsequence in strings centered around a given position
body: <p>In R I have a nx2 data frame where first column is a character vector (protein sequence) and second column a numeric  vector specifying a position in the character vector, e.g.:</p>
  
  <pre><code>test &lt;- data.frame(SEQ = c("LLKTISPAR", "KLKKRGVTTVK", "PAVLPTGGGLGTGTV"), 
                     POS = c(4,5,13))
  </code></pre>
  
  <p>I would like to retrieve the subsequence of a given size centered by the POS vector, which I managed to do with the function:</p>
  
  <pre><code>pep.windows &lt;- function(x, n){
      ifelse(!is.numeric(x[[2]]) | x[[2]] &lt; n +1 | n+x[[2]] &gt; nchar(as.character(x[[1]])) ,
             NA, 
             substring(x[[1]], x[[2]]-n, x[[2]]+n)) 
  }
  </code></pre>
  
  <p>Using the function to get windows 1 and 4 characters on each side of the POS character gives:</p>
  
  <pre><code>pep.windows(test, 1) 
  [1] "KTI" "KRG" "TGT"
  </code></pre>
  
  <p>and</p>
  
  <pre><code>pep.windows(test, 4) 
  [1] NA          "KLKKRGVTT" NA
  </code></pre>
  
  <p>My problem is that in the second example I want to make the function (say pep.windows2) return "X" for each position outside the window range, it would return:</p>
  
  <pre><code>&gt; pep.windows2(test, 4)
  [1] "XLKTISPA" "KLKKRGVTT" "GLGTGTVXX"
  </code></pre>
  
  <p>I cannot make this work. Would be nice to have it as a function that I could use apply on (something like <code>apply(test, 1, pep.windows2, ...)</code> ) - tried, couldnt make it work.</p>