同一链

时间:2015-10-30 00:03:06

标签: r dplyr

我正在尝试计算某个结果的概率(例如值沉降> = 3),但不知道如何在同一个链中合并tally和n。

这有效,但我不想依赖numsim

numsim=2
simdF %>%
     group_by(iter) %>%
     tally( precip >= 3 ) %>%
     mutate(
          prob=n/numsim
     )

为什么不:

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


     )

并且在那个注释中,如何使3成为包含该块的函数的参数?

谢谢!

示例数据:

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))

1 个答案:

答案 0 :(得分:2)

查看?tally

的文档
  

tallysummarise的便捷包装,可以根据... {/ p>}来调用nsum(n)

tally调用总结,因此将置于总结之内是没有意义的。只需直接转到n()sum(n) tally即可。在这种情况下,由于您有条件,请使用sum

simdF %>%
     group_by(iter) %>%
     summarise(
         freq = sum(precip >= 3),
         prob = freq/n()
     )

至于

  

如何使3成为包含此块的函数的参数

就像你提出任何争论一样:

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)