title: How to use custom functions in mutate (dplyr)?
body: <p>I'm rewriting all my code using dplyr, and need help with mutate / mutate_at function. All I need is to apply custom function to two columns in my table. Ideally, I would reference these columns by their indices, but now I can't make it work even referencing by names. </p>
  
  <p>The function is:</p>
  
  <pre><code>binom.test.p &lt;- function(x) {
    if (is.na(x[1])|is.na(x[2])|(x[1]+x[2])&lt;10) {
      return(NA)
    } 
    else {
      return(binom.test(x, alternative="two.sided")$p.value)
    }
  } 
  </code></pre>
  
  <p>My data: </p>
  
  <pre><code>table &lt;- data.frame(geneId=c("a", "b", "c", "d"), ref_SG1_E2_1_R1_Sum = c(10,20,10,15), alt_SG1_E2_1_R1_Sum = c(10,20,10,15))
  </code></pre>
  
  <p>So I do:</p>
  
  <pre><code>table %&gt;%
    mutate(Ratio=binom.test.p(c(ref_SG1_E2_1_R1_Sum, alt_SG1_E2_1_R1_Sum)))
  Error: incorrect length of 'x'
  </code></pre>
  
  <p>If I do:</p>
  
  <pre><code>table %&gt;% 
  mutate(Ratio=binom.test.p(ref_SG1_E2_1_R1_Sum, alt_SG1_E2_1_R1_Sum))
  Error: unused argument (c(10, 20, 10, 15))
  </code></pre>
  
  <p>The second error is probably because my function needs one vector and gets two parameters instead. </p>
  
  <p>But even forgetting about my function. This works:</p>
  
  <pre><code>table %&gt;%
    mutate(sum = ref_SG1_E2_1_R1_Sum + alt_SG1_E2_1_R1_Sum)
  </code></pre>
  
  <p>This doesn't:</p>
  
  <pre><code>    table %&gt;%
        mutate(.cols=c(2:3), .funs=funs(sum=sum(.)))
  Error: wrong result size (2), expected 4 or 1
  </code></pre>
  
  <p>So it's probably my misunderstanding of how dplyr works.</p>