title: switch out variable name using function argument in r
body: <p>I want to build a function that takes a variable name as an argument so if I have some code like:</p>
  
  <pre><code>function(df, variable = NULL) {
    df &lt;- df %&gt;% filter(variable &gt; 10)
    test &lt;- c()
    for (i in c(1:10)) {
      test2 &lt;- df$variable[i] - df$othervariable[i]
      test &lt;- c(test, test2)
    }
    return(test)
  }
  </code></pre>
  
  <p>Then I can swap out the variable simply by plugging in the name through the argument. I've thought about doing it like <code>df['variable']</code> instead. But this does not solve the problem in the dplyr filter function and also it returns a dataframe rather than <code>df$variable</code> which is numeric.</p>