2

I have the follow data setup

library(dplyr)
library(broom)

pop.mean = 0.10

df = data.frame( 
  trial    = as.integer(runif(1000, min = 5, max = 20)),
  success  = as.integer(runif(1000, min = 0, max = 20)),
  my.group = factor(rep(c("a","b","c","d"), each = 250))
)

I want to group on my.group and apply binom.test

bi.test <- df %>% group_by(my.group) %>%
  do(test = binom.test(sum(success),
                       sum(trial),
                       pop.mean,
                       alternative = c("two.sided"),
                       conf.level = 0.95))

Getting error message, cannot find success what am I doing wrong here?

iboboboru
  • 1,112
  • 2
  • 10
  • 21

2 Answers2

2

We need to extract the columns using $ within do

res <- df %>% 
          group_by(my.group) %>%
          do(test = binom.test(sum(.$success),
                   sum(.$trial),
                   pop.mean,
                   alternative = c("two.sided"),
                   conf.level = 0.95))

If we are using the broom functions, then

res1 <- df %>%
           group_by(my.group) %>%
           do(test = tidy(binom.test(sum(.$success),
                   sum(.$trial),
                   pop.mean,
                   alternative = c("two.sided"),
                   conf.level = 0.95)))

res1$test %>% 
    bind_rows %>% 
    bind_cols(res1[1], .)
# A tibble: 4 x 9
#  my.group  estimate statistic p.value parameter  conf.low conf.high              method alternative
#    <fctr>     <dbl>     <dbl>   <dbl>     <dbl>     <dbl>     <dbl>              <fctr>      <fctr>
#1        a 0.7908251      2310       0      2921 0.7756166 0.8054487 Exact binomial test   two.sided
#2        b 0.7525138      2320       0      3083 0.7368831 0.7676640 Exact binomial test   two.sided
#3        c 0.8446337      2479       0      2935 0.8310152 0.8575612 Exact binomial test   two.sided
#4        d 0.7901683      2395       0      3031 0.7752305 0.8045438 Exact binomial test   two.sided

NOTE: The dataset was created with a seed of 24 i.e. set.seed(24)

akrun
  • 874,273
  • 37
  • 540
  • 662
2

Thanks @akrun

I came up with a solution with tidyr::nest and purr::map after reading your answer.

res <- df %>%
  group_by(my.group) %>%
  tidyr::nest() %>%
  mutate(bi.test = 
           purrr::map(data, function(df) broom::tidy(
             binom.test(sum(df$success),
                        sum(df$trial),
                        pop.mean,
                        alternative = c("two.sided"),
                        conf.level = 0.95)))) %>%
  select(my.group, bi.test) %>%
  tidyr::unnest()
iboboboru
  • 1,112
  • 2
  • 10
  • 21