title: Does passing a whole dataframe to a function or just few columns make a difference in terms of computational speed?
body: <p>Suppose I have a data frame with a large number of columns</p>
  
  <pre><code>ncol = 40
  sample_size = 300
  
  my_matrix &lt;- replicate(ncol, runif(sample_size, 0, 3))
  my_df &lt;- data.frame(my_matrix)
  names(my_df) &lt;- paste0("x", 1:ncol)
  epsilon &lt;- rnorm(sample_size, 0, 0.2) 
  my_df$y &lt;- 1+3*my_df$x1 + epsilon
  </code></pre>
  
  <p>I pass the data frame to a function which needs only three of its columns to do the job (in my real codes, the function may use more than 3 columns, but I'm trying to keep things simple here):</p>
  
  <pre><code>library(ggplot2)
  
  idle_plotter &lt;- function(dataframe, x_string, y_string, color_string){
      p &lt;- ggplot(dataframe, aes_string(x = x_string, y = y_string, color = color_string)) +
          geom_point()
      print(p)
  }
  </code></pre>
  
  <p>Does it make a difference, in terms of speed, if I pass the whole <code>my_df</code> to  <code>idle_plotter</code>, or just the three columns <code>idle_plotter</code> needs? If the whole data frame is copied on call, I guess it does, but if R is pass-by-reference, it shouldn't. In my tests it doesn't seem to make a difference, but I need to know if:</p>
  
  <ul>
  <li>this is a rule, in which case I can continue to pass data frames to functions</li>
  <li>or just dumb luck, because the function is simple and/or the data frame is not huge. In this case I <strong>must</strong> abandon the habit of passing full data frames, or I risk making my code slower than it already is.</li>
  </ul>