I am trying to do chi-square test with a bunch of variables but I am getting errors. Could you please help me with this?
When I just work one by one it works. However, it is not working when I am using it in lapply. Does missingness is the main issue? How can I deal with this? Please see the code below:
> chisq.test(data$gender, data$crclgroup2_1mo)
Pearson's Chi-squared test with Yates' continuity correction
data: data$gender and data$crclgroup2_1mo
X-squared = 0.14166, df = 1, p-value = 0.7066
factor_variables <- c("gender", "race")
lapply(data[, factor_variables], function(x) chisq.test(data$crclgroup2_1mo, x))
Error is: Error in stats::chisq.test(x, y, ...) :
'x' and 'y' must have the same length