Q:

tally() and n() in same chain

I'm trying to calculate probability of a certain result (e.g. the value precip >=3) but don't know how to combine tally and n in the same chain.
this works but i'd like to not depend on numsim:
numsim=2
simdF %>%
     group_by(iter) %>%
     tally( precip >= 3 ) %>%
     mutate(
          prob=n/numsim
     )

why not:
simdF %>%
     group_by(iter) %>%
     summarise(
         freq=tally( precip >= 3 ),
         prob=freq/n()
 )

     )

and on that note, how can I make 3 be an argument to a function that contains this block?
Thanks!
sample data:
simdF=structure(list(nsim = c(1,2,1,2,1,2), iter = c(5, 5,10, 10, 30, 30), locE = c(-1, -2, -2, -1, 0, 4), locN = c(-1, 4, -2, -3, 0, 2), precip = c(1.4142135623731, 4.47213595499958, 2.82842712474619, 3.16227766016838, 0, 4.47213595499958)), .Names = c("nsim", "iter", "locE", "locN", "precip"), class = c("tbl_df", "data.frame"), row.names = c(NA, -6L))

A:

Looking at the documentation for ?tally

tally is a convenient wrapper for summarise that will either call n or sum(n) depending...

tally calls summarize, so it doesn't make sense to put it inside summarize. Just go directly to the n() or sum(n) that tally would. In this case, since you have a condition, use sum:
simdF %>%
     group_by(iter) %>%
     summarise(
         freq = sum(precip >= 3),
         prob = freq/n()
     )

As to

how can I make 3 be an argument to a function that contains this block

The same way you'd make anything an argument:
your_function = function(data, precip_lower_bound = 3) { 
  data %>%
       group_by(iter) %>%
       summarise(
           freq = sum(precip >= precip_lower_bound),
           prob = freq/n()
       )
}

your_function(data = simdF, precip_lower_bound = 3)

