title: Process dataframe with lapply and gsub
body: <p>I have a dataframe with different columns and especially a "commodity_name" column. In this column different commodities are listed, like "wti", "brent", "wheat", "cbot wheat", etc.</p>
  
  <p>Somehow I want to "filter" the column and group the names. So instead of "wti" and "brent" there shall be "group oil". It is also okay for me to have this in a extra column. 
  Background Info: I want to group the names and then "dummy" with the dummies library.</p>
  
  <p>I have tested different approaches so far, but none of them worked out. My idea right now ist the following.
  Setting up a list of commodities to be included in the "oil" group</p>
  
  <pre><code>oil &lt;- list("brent", "gasoline", "heating oil", "nymex heating oil", "nymex wti", "rbob gasoline",  "crude oil", "wti", "unleaded gas", "wti")
  </code></pre>
  
  <p>Setting up a function to prepare the names to be used with gsub. df = dataframe ("df_coded" in this case), findlist = my liste co commodities (oil in this case), replace = groupe name to replace the commodity name ("goil" in this case).</p>
  
  <pre><code>grouper &lt;- function(df, findlist, replace){
    string &lt;- df
    find.list &lt;- findlist
    find.string &lt;- paste(unlist(find.list), collapse ="|")
  
    df &lt;- gsub(find.string, replacement = replace, x = string)
  }
  </code></pre>
  
  <p>Finally, I used the grouper function with lapply on my dataframe column ("commodity_name")</p>
  
  <pre><code>df_coded$commodity_name &lt;- lapply(df_coded$commodity_name, function(x){grouper(df_coded$commodity_name, oil, "goil")})
  </code></pre>
  
  <p>Now my whole program gets stuck.
  There must be an easier / better way for this problem. 
  Thank you in advance for your help!</p>