title: How to move patterns in a string in r?
body: <p>I am trying to code a function that would allow me to move certain patterns in a string in r. For example, if my strings are pattern_string1, pattern_string2, pattern_string3, pattern_string4, I want to mutate them to string1_pattern, string2_pattern, string3_pattern, string4_pattern.</p>
  <p>In oder to achieve this, I tried the following:</p>
  <pre><code>string_flip &lt;- function(x, pattern){
    if(str_detect(x, pattern)==TRUE){
      str_remove(x, pattern) %&gt;%
      paste(x, &quot;pattern&quot;, sep = &quot;_&quot;)
    }
  }
  </code></pre>
  <p>However, when I try to apply this onto a vector of strings by the following code:</p>
  <pre><code>stringvector &lt;- c(pattern_string1, pattern_string2, pattern_string3, pattern_string4, string5, string6)
  string_flip(stringvector, &quot;pattern&quot;)
  </code></pre>
  <p>it returns a warning and changes all vectors, not only the vectors that contain &quot;pattern&quot;. In addition it does not only add pattern to the end of the string, it doubles the string itself as well, so I get the following result:</p>
  <pre><code>[1] &quot;_string1_pattern_string1_pattern&quot; &quot;_string2_pattern_string2_pattern&quot; &quot;_string3_pattern_string3_pattern&quot;
  [4] &quot;_string4_pattern_string4_pattern&quot; &quot;string5_string5_pattern&quot;                &quot;string6_string6_pattern&quot;  
  </code></pre>
  <p>Can anybody help me with this?
  Thanks a lot in advance!</p>