# install.packages("MASS") library(MASS) print(str(survey)) ??MASS::survevy wh.clap = data.frame(survey$Clap,survey$W.Hnd) wh.clap.tbl = table(survey$Clap,survey$W.Hnd) wh.clap.tbl chisq <- chisq.test(wh.clap.tbl) chisq o <- chisq$observed e <- chisq$expected o round(e, 2) res <- (e-o)/sqrt(e) round(res,2) chisq.cal <- sum(res^2) p.val <- pchisq(chisq.cal, df=2, lower.tail = F) chisq.cal p.val chisq barplot(wh.clap.tbl, beside = TRUE, col = c("red", "lightgreen", "blue"), main = "wh vs ex", xlab = "Exercise Level", ylab = "Number of Students") legend("center", legend = rownames(wh.clap.tbl), fill = c("red", "lightgreen" ,"blue"))