title: R: dplyr pipe conditional lead/lag using ifelse with unexpected behavior
body: <p>I'm trying to use a conditional <code>lead</code>/<code>lag</code> function in a dplyr pipe using ifelse but getting an error. However, using the same approach outside the pipe it seems to work. What am I missing?   </p>
  
  <pre><code>require(dplyr)
  </code></pre>
  
  <p>Data:</p>
  
  <pre><code>test &lt;- data.frame(a = c("b","b","b","b","b","b",
                           "m","m","m","m","m","m",
                           "s","s","s","s","s","s"), 
                     b = replicate(1,n=18), 
                     stringsAsFactors=F)
  </code></pre>
  
  <p>dplyr pipe:</p>
  
  <pre><code>test %&gt;%
    mutate(delta = ifelse(a == "s", b + lag(b, n = 2*6),
                          ifelse(a == "m", b + lag(b, n = 1*6), 0)))
  
  # Error: could not convert second argument to an integer. type=LANGSXP, length = 3
  </code></pre>
  
  <p>Without the pipe it works:</p>
  
  <pre><code>test$delta &lt;- ifelse(test$a == "s", test$b + lag(test$b, n = 2*6),
                       ifelse(test$a == "m", test$b + lag(test$b, n = 1*6), 0))
  </code></pre>
  
  <p>I found some indication that there was an issue with dplyr <code>lead</code>/<code>lag</code> in combination with grouped data frames. But I am not grouping here.</p>
  
  <p>Version info: R 3.1.1 and dplyr_0.4.1.</p>