title: R - fail to store multiple output from for loop in vector or data frame
body: <p>I have a data frame called <code>lbt_all_epitopes</code> of 38282 rows and three columns, as shown below:</p>
  
  <pre><code> sequence    score epitope.
  1 RPGGPPGYRTPYTAK 1.724911  Epitope
  2 TQGDRQKIQDAVSAA 1.664611  Epitope
  3 EVKSRYNVDVSQNKR 1.593236  Epitope
  4 VIEMTRAFEDDDFDK 1.578200  Epitope
  5 ITQGDRQKIQDAVSA 1.533208  Epitope
  6 GSADLTPSNLTRPAS 1.532700  Epitope
  </code></pre>
  
  <p>In the first column (named sequence) I have multiple similar strings, which I want to remove (I will be looking for similar strings using <code>str_sub</code>). For example, considering the first string of <code>lbt_all_epitopes$sequence</code> ("RPGGPPGYRTPYTAK") I want to look for similar strings in the whole column and store them in a <code>vector</code> or in a <code>data.frame</code>, which will be called <code>to_be_removed</code>. I want to do this iteration for the first 30 elements present in <code>lbt_all_epitopes$sequence</code>. For the sake of  simplicity, let's just consider the top five rows. When I run the loop, like the one below:</p>
  
  <pre><code># Iterate over the first 5 rows
  top_30 &lt;- 1:5
  
  for(i in top_30) {
    print(agrep(str_sub(lbt_all_epitopes$sequence[i], start = 5, end = 11), lbt_all_epitopes$sequence, value = T))
  }
  </code></pre>
  
  <p>The output: </p>
  
  <pre><code> [1] "RPGGPPGYRTPYTAK" "VGTRPGGPPGYRTPY" "TRPGGPPGYRTPYTA" "GGPPGYRTPYTAKPF" "PGGPPGYRTPYTAKP"
   [6] "LVGTRPGGPPGYRTP" "TLVGTRPGGPPGYRT" "GPPGYRTPYTAKPFV" "PPGYRTPYTAKPFVM" "GTRPGGPPGYRTPYT"
  [11] "PGYRTPYTAKPFVMC"
   [1] "TQGDRQKIQDAVSAA" "ITQGDRQKIQDAVSA" "GITQGDRQKIQDAVS" "NGITQGDRQKIQDAV" "QGDRQKIQDAVSAAS"
   [6] "QNGITQGDRQKIQDA" "GDRQKIQDAVSAASS" "VQNGITQGDRQKIQD" "DRQKIQDAVSAASSW" "RQKIQDAVSAASSWL"
  [11] "QKIQDAVSAASSWLE"
   [1] "EVKSRYNVDVSQNKR" "VKSRYNVDVSQNKRA" "NEVKSRYNVDVSQNK" "KSRYNVDVSQNKRAR" "LNEVKSRYNVDVSQN"
   [6] "YNVDVSQNKRARLRL" "RYNVDVSQNKRARLR" "MLNEVKSRYNVDVSQ" "SRYNVDVSQNKRARL" "HMLNEVKSRYNVDVS"
  [11] "EHMLNEVKSRYNVDV"
   [1] "VIEMTRAFEDDDFDK" "RVIEMTRAFEDDDFD" "GDRVIEMTRAFEDDD" "DRVIEMTRAFEDDDF" "IEMTRAFEDDDFDKF"
   [6] "RGDRVIEMTRAFEDD" "EMTRAFEDDDFDKFD" "FRGDRVIEMTRAFED" "MTRAFEDDDFDKFDR" "TRAFEDDDFDKFDRV"
  [11] "RAFEDDDFDKFDRVR"
   [1] "TQGDRQKIQDAVSAA" "ITQGDRQKIQDAVSA" "GITQGDRQKIQDAVS" "NGITQGDRQKIQDAV" "QGDRQKIQDAVSAAS"
   [6] "QNGITQGDRQKIQDA" "GDRQKIQDAVSAASS" "VQNGITQGDRQKIQD" "DVQNGITQGDRQKIQ" "DRQKIQDAVSAASSW"
  [11] "RQKIQDAVSAASSWL"
  </code></pre>
  
  <p>Is exactly what I want i.e. it printed all the similar strings (11 per iteration) to the first, second, third...fifth elements of <code>lbt_all_epitopes$sequence</code>.
  However, when I try to store the output in a vector (called <code>to_be_removed</code>), with the following loop:</p>
  
  <pre><code># create the empty vector where I will store the output
  to_be_removed &lt;- c()
  
  for(i in top_30) {
    to_be_removed[i] &lt;- agrep(str_sub(lbt_all_epitopes$sequence[i], start = 5, end = 11), lbt_all_epitopes$sequence, value = T)
  }
  </code></pre>
  
  <p>I noticed that each iteration produced only a single string as output (as opposed to 11 strings for each iteration), as below:</p>
  
  <pre><code>&gt; to_be_removed
  [1] "RPGGPPGYRTPYTAK" "TQGDRQKIQDAVSAA" "EVKSRYNVDVSQNKR" "VIEMTRAFEDDDFDK" "TQGDRQKIQDAVSAA"
  </code></pre>
  
  <p>The following warning message was displayed:</p>
  
  <pre><code>Warning messages:
  1: In to_be_removed[i] &lt;- agrep(str_sub(lbt_all_epitopes$sequence[i],  :
    number of items to replace is not a multiple of replacement length
  2: In to_be_removed[i] &lt;- agrep(str_sub(lbt_all_epitopes$sequence[i],  :
    number of items to replace is not a multiple of replacement length
  3: In to_be_removed[i] &lt;- agrep(str_sub(lbt_all_epitopes$sequence[i],  :
    number of items to replace is not a multiple of replacement length
  4: In to_be_removed[i] &lt;- agrep(str_sub(lbt_all_epitopes$sequence[i],  :
    number of items to replace is not a multiple of replacement length
  5: In to_be_removed[i] &lt;- agrep(str_sub(lbt_all_epitopes$sequence[i],  :
    number of items to replace is not a multiple of replacement length
  </code></pre>
  
  <p>I am then assuming that I am missing the code telling R that it should also concatenate all the strings produced by each iteration, then go to the next iteration. 
  Does anyone know how to correctly store the output in a <code>vector</code>, or even in a <code>data.frame</code>?</p>