title: Replace or remove certain characters of string in a R data frame column from lookup table
body: <p>I have a dataset with unique IDs and a corresponding path field where the path is a sequential number of events (think: purchased item1, item2, item3).</p>
  
  <p>Here is some code to develop some simple sample data:</p>
  
  <pre><code>PathStackOverflow &lt;- c("a,b,c","b,c","a,d,e","d,c","e","a,e","a,b,c,e")
  PathStackOverflow
  [1] "a,b,c"   "b,c"     "a,d,e"   "d,c"     "e"       "a,e"     "a,b,c,e"
  PathID &lt;- c(123,144,156,133,153,122,169)
  PathID
  [1] 123 144 156 133 153 122 169
  StackOverflowDF &lt;- cbind(PathID,PathStackOverflow)
  StackOverflowDF &lt;- data.frame(cbind(PathID,PathStackOverflow))
  View(StackOverflowDF)
  
  
  PathID PathStackOverflow
  1    123             a,b,c
  2    144               b,c
  3    156             a,d,e
  4    133               d,c
  5    153                 e
  6    122               a,e
  7    169           a,b,c,e
  </code></pre>
  
  <p>The actual data frame has ~350,000 observations so it would be great if there's a computationally quick solution!</p>
  
  <p>Some of the path values (e.g. a or e) need to be totally deleted based off a look up table. For example, let's say I want to remove all records with b  and d above. I want to define a vector for b and d and then remove all instances of that from the column so the end result looks like:</p>
  
  <pre><code> PathID PathStackOverflow
  1    123             a,c
  2    144               c
  3    156             a,e
  4    133               c
  5    153               e
  6    122             a,e
  7    169           a,c,e
  </code></pre>
  
  <p>It's important that the integrity of the column and dataframe is conserved because the next step is a cSplit where I'm splitting out the path by the comma and running some probabilities and frequencies of the appearance of each value. </p>
  
  <p>So far, I've tried to do a horrible inefficient series of steps:</p>
  
  <ol>
  <li>using grepl in ifelse to flag records for each instance where a path
  value needs to be deleted </li>
  <li>then running cSplit to split out into several columns I've tried to do row level split/combine operations with plyr to find if columns in each row has the value and if so, delete them and recombine that row on the original. </li>
  </ol>
  
  <p>But haven't been able to figure it out. Besides I think this approach is not efficient and there has to be a way to do delete the strings from the original, concatenated column.</p>
  
  <p>Also tried the Gsub approach:</p>
  
  <pre><code>StackOverflowDF$ID &lt;- gsub(',+b', '', StackOverflowDF$PathStackOverflow)
  StackOverflowDF$ID &lt;- gsub('b+,', '', StackOverflowDF$PathStackOverflow)
  </code></pre>
  
  <p>This seemed to work better but then my code is inefficient as I'm having to copy paste multiple lines for each char I want to remove and for every comma combo. </p>
  
  <p><strong>I'm thinking the right solution would be some sort of helper function that can take the vector of strings to remove e.g. RemoveChars &lt;- c("b","d") and then perform the gSub actions for each type of comma combo? Not sure if that's the best way to proceed and even if it is, how to loop through each look up table value.</strong> </p>
  
  <p>Would appreciate any help on how to proceed with this. </p>