title: How to split column by some rules in R?
body: <p>I need to split the column into two columns.</p>
  <p>Here is the input data set</p>
  <pre><code>df_in &lt;- data.table(
    product = c(&quot;Product 1 - 100g&quot;, &quot;Product 2 - 150g&quot;, &quot;Product 3&quot;, &quot;Product 4 - keep it&quot;),
    tags = c(NA, NA, NA, NA))
  </code></pre>
  <p>The data after &quot; - &quot; should be extracted into the &quot;tags&quot; column. But not for all rows. Sometimes there are products with &quot;-&quot; we need to keep. I put these strings into separate list</p>
  <pre><code>list_of_keep_data &lt;- c(&quot;keep it&quot;)
  </code></pre>
  <p>So, the output data set should be the following:</p>
  <pre><code>df_out &lt;- data.table(
    product = c(&quot;Product 1&quot;, &quot;Product 2&quot;, &quot;Product 3&quot;, &quot;Product 4 - keep it&quot;),
    tags = c(&quot;100g&quot;, &quot;150g&quot;, NA, NA))
  </code></pre>
  <p>Will be glad to see possible solutions by using &quot;str_split&quot;, &quot;str_location&quot; or any other functions in a tidyverse style.</p>
  <p>Thanks!</p>