title: R apply custom vectorised function to row in dataframe, specific columns
body: <p>This should be simple but I just can't get the <code>apply</code> to communicate with my vectorised function.</p>
  
  <p>Test data is: <code>df &lt;- data.frame(a = 1:3, b1 = c(4:5, NA), b2 = c(5,6,5))</code>
  Looks like this:</p>
  
  <pre><code>  a b1 b2
  1 1  4  5
  2 2  5  6
  3 3 NA  5
  </code></pre>
  
  <p>Custom function checks returns a vector to indicate whether values fall in a given interval.</p>
  
  <pre><code>validScore &lt;- function(x, a, b) {
    is.na(x) == FALSE &amp;
    x%%1 == 0 &amp;
    findInterval(x, c(a,b), rightmost.closed = TRUE) == 1
  }
  </code></pre>
  
  <p>Test of custom function: <code>validScore(c(3, 3.5, 6, NA), 1, 5)</code> returns the logical vector <code>TRUE FALSE FALSE FALSE</code> as expected.</p>
  
  <p>I want to run the custom function on the row defined by the columns b1 and b2. This would return TRUE FALSE FALSE (that is T on (b1=4,b2=5), F on (b1=5,b2=6) and F on (b1=NA,b2=5)).</p>
  
  <p>The answer <a href="https://stackoverflow.com/questions/15059076/r-how-to-call-apply-like-function-on-each-row-of-dataframe-with-multiple-argum">Call apply-like function on each row of dataframe with multiple arguments from each row</a> for selecting the columns, and <a href="https://stackoverflow.com/questions/4236368/how-to-apply-a-function-to-every-row-of-a-matrix-or-a-data-frame-in-r">how to apply a function to every row of a matrix (or a data frame) in R</a> together suggest the following:</p>
  
  <pre><code>library(dplyr)
  apply(select(df, b1:b2), 1, function(x) validScore(x, 1, 5))
  </code></pre>
  
  <p>but that doesn't actually send the row to the function, instead assessing each value individually, so output is:</p>
  
  <pre><code>   [,1]  [,2]  [,3]
  b1 TRUE  TRUE FALSE
  b2 TRUE FALSE  TRUE
  </code></pre>
  
  <p>Sticking a rowwise() into the middle like <code>select(df, b1:b2) %&gt;% rowwise() %&gt;% apply(1, function(x) validScore(x, 1, 5))</code> makes no difference.</p>
  
  <p>I thought it might by something to do with the form that the dplyr select returned, but <code>apply(df[, c("b1", "b2")], 1, function(x) validScore(x, 1, 5))</code> also generates the same result.</p>