title: Define global variable using function argument in R
body: <p>I'm trying to write a function in R that drops columns from a data frame and returns the new data with a name specified as an argument of the function:</p>
  
  <pre><code>drop &lt;- function(my.data,col,new.data) {
  new.data &lt;&lt;- my.data[,-col] 
  return(new.data)
  }
  </code></pre>
  
  <p>So in the above example, I want a new data frame to exist after the function is called that is named whatever the user inputs as the third argument.</p>
  
  <p>When I call the function the correct data frame is returned, but then if I then try to use the new data frame in the global environment I get <code>object not found</code>. I thought by using the <code>&lt;&lt;-</code> operator I was defining <code>new.data</code> globally.</p>
  
  <p>Can someone help me understand what's going on and if there is a way to accomplish this?</p>
  
  <p>I found <a href="https://stackoverflow.com/questions/10904124/global-and-local-variables-in-r">this</a> and <a href="https://stackoverflow.com/questions/6330468/how-can-i-define-a-global-variable-text-in-r">this</a> that seemed related, but neither quite answered my question.</p>