title: create a new column in R using a user defined function
body: <p>I am trying to do pattern matching of a column and make a new column with a part of the matched pattern using a user-defined function.</p>
  
  <p>Table name is data , it contains</p>
  
  <pre><code>MatchedColumn
  24 abcd
  32 gcef
  45 lmno
  </code></pre>
  
  <p>Code is</p>
  
  <pre><code>f4&lt;-function()
  {
    data$NewColumn &lt;- gsub('([0-9]{2}) *','\\1',data$MatchedColumn)
  }
  </code></pre>
  
  <p>and i am calling the function as f4(), but it is not executing the inside part of the function.</p>
  
  <p>But when i run the below line alone its creating a new column after matching and the produced table is as shown.</p>
  
  <pre><code>data$NewColumn &lt;- gsub('([0-9]{2}) *','\\1',data$MatchedColumn)
  </code></pre>
  
  <p>data: </p>
  
  <pre><code>MatchedColumn  NewColumn
   24 abcd         24
   32 gcef         32
   45 lmno         45
  </code></pre>
  
  <p>I am trying to use user defined function because i want to pass <strong>NewColumn</strong> and <strong>MatchedColumn</strong> name through function, something like this f4(NewColumn,MatchedColumn)</p>
  
  <p>Please help if anyone knows how to do it!</p>