I'm trying to create a function that contains the table() function, but I keep returning the error "Error in table(df$var, df$arm) : all arguments must have the same length ". I've played around with every alternative option to referencing the columns that I can think of but to no avail.
Here is a reproduceable example:
x <- c(0, 1, 0, 1)
arm <- c("A", "A", "B", "B")
study.df <- data.frame(x, arm, row.names = NULL)
f.table <- function(df, var) {
p <- table(df$var, df$arm) %>%
fisher.test(alternative = "two.sided") %>%
.$p.value
return(p)
}
f.table(study.df, x)