title: Append strings to a text in R
body: <p>I want to classify some characters that fulfill a condition in one column and concatenate the other characters in a string in another column.</p>
  
  <p>The classification is working. When there is a 1 in the column "col", the program has to compare the inputs in "Category", the actual value with the previous one. If the priority number is smaller, save the value in "AlarmPrior", and the other value in "Other Alarms". I want to concatenate all the values with less priority in a string in "Other Alarms".</p>
  
  <pre class="lang-r prettyprint-override"><code>    #test the function 
  
      col &lt;- c(0, 1, 0, 0, 1, 1)
      Priority &lt;- c(1,2,3,4,5,6)
      Category &lt;- c("a","b","c","d","e","f")
      eventlog_overlap.dt &lt;- data.table(col,Priority, IEC_category)
  
      #loading the libraries
      library(magrittr) 
      library(dplyr)
  
      #comparison and value assignation in function of the priority
  
      eventlog_overlap.dt$OtherAlarms &lt;- "" 
      eventlog_overlap.dt &lt;- 
        eventlog_overlap.dt %&gt;% 
          mutate(AlarmPrior = ifelse(col == 1,
                                        ifelse(Priority &lt;= lag(Priority), 
                                                 Category, 
                                                 lag(Category)), NA),
                 OtherAlarms = ifelse(col == 1,
                                      ifelse(Priority &lt;= lag(Priority),
                                                 "1",
                                                 paste0(sprintf(Category,     lag(OtherAlarms)), collapse = ", ")),NA))
  
  </code></pre>
  
  <p>For example:</p>
  
  <p>This input,</p>
  
  <pre><code>col &lt;- c(0, 1, 0, 0, 1, 1)
  Priority &lt;- c(1,2,3,4,5,6)
  Category &lt;- c("a","b","c","d","e","f")
  </code></pre>
  
  <p>Should return:</p>
  
  <pre class="lang-r prettyprint-override"><code>             col      Priority      Category     OtherAlarms     AlarmPrior
      1         0          1             a             NA             NA
      2         1          2             b             b               a             
      3         0          3             c             b,c             NA
      4         0          4             d             b,c             NA
      5         1          5             e             b,c,e           d
      6         1          6             f             b,c,e,f         e
  
  </code></pre>
  
  <p>My actual result is this one:</p>
  
  <pre class="lang-r prettyprint-override"><code>
               col      Priority      Category     OtherAlarms     AlarmPrior
      1         0          1             a             NA             NA
      2         1          2             b             a,b,c,d,e,f     a             
      3         0          3             c             NA              NA
      4         0          4             d             NA              NA
      5         1          5             e             a,b,c,d,e,f     d
      6         1          6             f             a,b,c,d,e,f     e
  
  </code></pre>