Chi square test in loop

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

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.