我想对泰伯的每一行进行泊松测试。但是,变异失败,因为poisson.test似乎是为数字而不是列设计的。什么是最好的解决办法?目前,我唯一能做到的就是一个明确的for循环.
df <- tribble(
~count, ~time,
5, 10,
4, 7,
8, 10)
# Fails
df %>% mutate(rate = poisson.test(count, T = time)
# Error in poisson.test(remove_count, T = FHs) :
# the case k > 2 is unimplemented发布于 2019-10-10 18:02:28
由于我们希望在每一行上应用,一个选项是map2,其中它接受'count‘、'time’的每个元素并执行poisson.test
library(dplyr)
library(purrr)
df %>%
mutate(rate = map2(count, time, poisson.test))或者如果我们不想加载另一个包(purrr),那么这可以用rowwise来完成
df %>%
rowwise %>%
mutate(rate = list(poisson.test(count, time))) 这将使测试输出作为list列。如果我们对提取estimate感兴趣
df %>%
mutate(rate = map2_dbl(count, time, ~poisson.test(.x, .y)$estimate))
# A tibble: 3 x 3
# count time rate
# <dbl> <dbl> <dbl>
#1 5 10 0.5
#2 4 7 0.571
#3 8 10 0.8 https://stackoverflow.com/questions/58328656
复制相似问题