title: R: Pass data.frame by reference to a function
body: <p>I pass a <code>data.frame</code> as parameter to a function that want to alter the data inside: </p>
  
  <pre><code>x &lt;- data.frame(value=c(1,2,3,4))
  f &lt;- function(d){
    for(i in 1:nrow(d)) {
      if(d$value[i] %% 2 == 0){
        d$value[i] &lt;-0
      }
    }
    print(d)
  }
  </code></pre>
  
  <p>When I execute <code>f(x)</code> I can see how the <code>data.frame</code> inside gets modified:</p>
  
  <pre><code>&gt; f(x)
    value
  1     1
  2     0
  3     3
  4     0
  </code></pre>
  
  <p>However, the original <code>data.frame</code> I passed is unmodified:</p>
  
  <pre><code>&gt; x
    value
  1     1
  2     2
  3     3
  4     4
  </code></pre>
  
  <p>Usually I have overcame this by returning the modified one:</p>
  
  <pre><code>f &lt;- function(d){
    for(i in 1:nrow(d)) {
      if(d$value[i] %% 2 == 0){
        d$value[i] &lt;-0
      }
    }
    d
  }
  </code></pre>
  
  <p>And then call the method reassigning the content:</p>
  
  <pre><code>&gt; x &lt;- f(x)
  &gt; x
    value
  1     1
  2     0
  3     3
  4     0
  </code></pre>
  
  <p>However, I wonder what is the effect of this behaviour in a very large <code>data.frame</code>, is a new one grown for the method execution? Which is the R-ish way of doing this?</p>
  
  <p>Is there a way to modify the original one without creating another one in memory?</p>