I am trying to run excact binom.tests over a data frame with multiple rows with the following function and call, as previously presented by @Joran:
get_binCI <- function(x, n) {
bi <- binom.test(x,n)$conf.int*100
data_frame(lwr = bi[1],
upr = bi[2])
}
library(dplyr)
df %>%
group_by(col1,col2) %>%
do(get_binCI(.$col1, .$col2)
It works on the following data frame:
col1 <- c(11,2,7,6,5,2,3,4,1,1,5,8,7,6)
col2 <- c(303,303,303,303,303,471,471,471,471,200,200,200,200,200)
df <- data.frame(col1,col2, stringsAsFactors = F)
However, when I try to use it on my own data frame (with multiple other columns and significantly longer than the one above, but otherwise similar), I get the following error:
Error in binom.test(x,n) : incorrect length of 'x'
So i looked at the source code for the binom.test (below), and found where the error comes from, but I cannot seem to solve the issue...
DNAME <- deparse(substitute(x))
xr <- round(x)
if (any(is.na(x) | (x < 0)) || max(abs(x - xr)) > 1e-07)
stop("'x' must be nonnegative and integer")
x <- xr
if (length(x) == 2L) {
n <- sum(x)
x <- x[1L]
}
else if (length(x) == 1L) {
nr <- round(n)
if ((length(n) > 1L) || is.na(n) || (n < 1) || abs(n -
nr) > 1e-07 || (x > nr))
stop("'n' must be a positive integer >= 'x'")
DNAME <- paste(DNAME, "and", deparse(substitute(n)))
n <- nr
}
else stop("incorrect length of 'x'")
As the only difference between the two data frames is the length and the additional columns, I have no clue as to why this error is produced.