title: Why doesn't dplyr::tally read variables in a function?
body: <p>dplyr::tally is faster than dplyr::count. Why doesn't tally read alpha variables in a function? </p>
  
  <p>for sample x for this example say:</p>
  
  <pre><code>x &lt;- data.frame("PrecinctID" = c(101,102,103,104))
  
  
  tally(x,PrecinctID == 101)[1,1] 
  
  #[1] 919 
  
  findy &lt;- function(y) {tally(x,PrecinctID == y)[1,1]} 
  
  findy(101)
  
  #Error: object 'y' not found
  
  findy &lt;- function(y) {count(x,PrecinctID == y)[2,2]} 
  
  findy(101)  
  
  #Source: local data frame [1 x 1]
  
  #    n
  #1 919
  </code></pre>
  
  <p>[Self answer:]</p>
  
  <p>I was able to solve my own problem. Tally accepts only tbl data. So whether you use tally or summarise, it works well to pump it through dplyr pipe (%>%) or "then" operator. Once you do that, quite complex fields embedded with queries can be orchestrated. Given x is large voter database:</p>
  
  <pre><code>tbl_df(x)
  Source: local data frame [128,438 x 17] ...
  
  StateVoterID RegistrationNumber LastName FirstName ...
  
  uPID &lt;- sort(unique(x$PrecinctID))  
  findP &lt;- function(y) {  
  x %&gt;%  
  summarise(  
  Count = sum(PrecinctID == y),  
  Good = sum(AVReturnStatus == "Good" &amp; PrecinctID == y),  
  Late = sum(AVReturnChallenge == "Too Late" &amp; PrecinctID == y))  
  }  
  
  u1 &lt;- t(sapply(uPID,findP))  
  u1 &lt;- cbind(uPID,u1)  
  
  
  head(u1)  
       uPID Count Good Late  
  [1,] 101  917   476  4   
  [2,] 102  630   367  8   
  [3,] 103  687   482  2   
  [4,] 104  439   312  1   
  [5,] 105  414   252  0   
  [6,] 106  778   422  2   
  </code></pre>