I think you're over-complicating things by using substitute
and such. Without your data, I'll try with mtcars
, using cyl
as the exposure variable.
data <- mtcars
name <- names(data)
ev <- "cyl"
varlist <- name[ name != ev ]
models <- lapply(varlist, function(nm) {
chisq.test(table(data[[nm]], data[[ev]]))
})
# Warning messages:
# 1: In chisq.test(table(data[[nm]], data[[ev]])) :
# Chi-squared approximation may be incorrect
(because I'm using a bad example for the test, there are a lot of warnings here; this can be ignored when using mtcars
because it is really not a good dataset for this test.)
summaries <- lapply(models, summary)
str(summaries[1:2])
# List of 2
# $ : 'summaryDefault' chr [1:9, 1:3] " 1" " 1" " 1" " 1" ...
# ..- attr(*, "dimnames")=List of 2
# .. ..$ : chr [1:9] "statistic" "parameter" "p.value" "method" ...
# .. ..$ : chr [1:3] "Length" "Class" "Mode"
# $ : 'summaryDefault' chr [1:9, 1:3] " 1" " 1" " 1" " 1" ...
# ..- attr(*, "dimnames")=List of 2
# .. ..$ : chr [1:9] "statistic" "parameter" "p.value" "method" ...
# .. ..$ : chr [1:3] "Length" "Class" "Mode"