title: Why is a function giving me a different answer from the code without the function?
body: <p>So this is the code chunk I am trying to put in a custom function</p>
  <pre><code>treated.median &lt;- with(subset(dta, catholic == 1), Hmisc::wtd.quantile(score, probs = .5)) 
  counterfactual &lt;- with(subset(dta, catholic == 0), Hmisc::wtd.quantile(score, ipw_tot, probs = .5))
  QTET &lt;- treated.median - counterfactual
  QTET
  </code></pre>
  <p>the output this gives me when running it is</p>
  <pre><code>50% 
  -1.083 
  </code></pre>
  <p>I tried to make it a function like this</p>
  <pre><code>ZIB &lt;- function(data, k, s, t) {
    treated.median &lt;- with(data[k == 1,], Hmisc::wtd.quantile(s, probs = .5)) 
    counterfactual &lt;- with(data[k == 0,], Hmisc::wtd.quantile(s, t, probs = .5))
    QTET &lt;- treated.median - counterfactual
    return(QTET)
  }
  
  ZIB(dta, 
       dta$catholic, dta$score, dta$ipw_tot)
  </code></pre>
  <p>the output I get is this</p>
  <pre><code>  50% 
  -2.397 
  </code></pre>
  <p>What am I missing here? Why am I getting two different answers? (I have a feeling that it might be a very silly thing that I am missing).</p>