Here's a tidyverse
approach, illustrated with fake data:
library(tidyverse)
library(broom)
# Fake data
set.seed(2)
dat = data.frame(village=rep(LETTERS[1:24], each=100),
PCR=sample(c("+","-"), 100*24, replace=TRUE))
# Set "+" to be the reference level
dat$PCR = factor(dat$PCR, levels=c("+","-"))
Now, the code below first splits dat
by village
and feeds the result to map_df
. map_df
runs binom.test
on the data for each level of village
. binom.test
returns a list, but wrapping it in tidy
turns the output of binom.test
into a "tidy" data frame. So we end up with a data frame where each row is the output of binom.test
for one village.
split(dat, dat$village) %>%
map_df(~ tidy(binom.test(table(.x$PCR), p=0.5,
alternative = c("two.sided", "less", "greater"),
conf.level = 0.95)), .id="Village")
binom.test.results
Village estimate statistic p.value parameter conf.low conf.high method alternative
1 A 0.55 55 0.36820162 100 0.4472802 0.6496798 Exact binomial test two.sided
2 B 0.54 54 0.48411841 100 0.4374116 0.6401566 Exact binomial test two.sided
3 C 0.48 48 0.76435343 100 0.3790055 0.5822102 Exact binomial test two.sided
4 D 0.55 55 0.36820162 100 0.4472802 0.6496798 Exact binomial test two.sided
5 E 0.43 43 0.19334790 100 0.3313910 0.5328663 Exact binomial test two.sided
6 F 0.47 47 0.61729941 100 0.3694052 0.5724185 Exact binomial test two.sided
7 G 0.49 49 0.92041076 100 0.3886442 0.5919637 Exact binomial test two.sided
8 H 0.49 49 0.92041076 100 0.3886442 0.5919637 Exact binomial test two.sided
9 I 0.53 53 0.61729941 100 0.4275815 0.6305948 Exact binomial test two.sided
10 J 0.50 50 1.00000000 100 0.3983211 0.6016789 Exact binomial test two.sided
11 K 0.49 49 0.92041076 100 0.3886442 0.5919637 Exact binomial test two.sided
12 L 0.50 50 1.00000000 100 0.3983211 0.6016789 Exact binomial test two.sided
13 M 0.45 45 0.36820162 100 0.3503202 0.5527198 Exact binomial test two.sided
14 N 0.59 59 0.08862608 100 0.4871442 0.6873800 Exact binomial test two.sided
15 O 0.41 41 0.08862608 100 0.3126200 0.5128558 Exact binomial test two.sided
16 P 0.50 50 1.00000000 100 0.3983211 0.6016789 Exact binomial test two.sided
17 Q 0.54 54 0.48411841 100 0.4374116 0.6401566 Exact binomial test two.sided
18 R 0.52 52 0.76435343 100 0.4177898 0.6209945 Exact binomial test two.sided
19 S 0.44 44 0.27125302 100 0.3408360 0.5428125 Exact binomial test two.sided
20 T 0.55 55 0.36820162 100 0.4472802 0.6496798 Exact binomial test two.sided
21 U 0.47 47 0.61729941 100 0.3694052 0.5724185 Exact binomial test two.sided
22 V 0.52 52 0.76435343 100 0.4177898 0.6209945 Exact binomial test two.sided
23 W 0.54 54 0.48411841 100 0.4374116 0.6401566 Exact binomial test two.sided
24 X 0.51 51 0.92041076 100 0.4080363 0.6113558 Exact binomial test two.sided
dput(data_sample)
) it will be easier to help you. – eipi10