For example I have a dataset
structure(list(`total primary - yes RS` = c(138L, 101L, 86L,
118L), `total primary - no RS` = c(29L, 39L, 35L, 38L), `total secondary- yes rs` = c(6L,
15L, 3L, 15L), `total secondary- no rs` = c(0L, 7L, 1L, 2L)), row.names = c(NA,
-4L), class = c("tbl_df", "tbl", "data.frame"))
I am able to run each row individually and get the information i want for example
Result<-tidy(chisq.test(matrix(unlist(df[1,]), ncol = 2)))
Result2<-tidy(chisq.test(matrix(unlist(df[2,]), ncol = 2)))
Result3<-tidy(chisq.test(matrix(unlist(df[3,]), ncol = 2)))
Result4<-tidy(chisq.test(matrix(unlist(df[4,]), ncol = 2)))
but i would like to run it in a loop and not have to repeat the same line of code 4 times.My goal is for something like this.
statistic|p.value|parameter|method
0.3165439 0.5736921 1 Pearson's Chi-squared test with Yates' continuity correction
0.01656976 0.8975764 1 Pearson's Chi-squared test with Yates' continuity correction
6.698956e-32 1 1 Pearson's Chi-squared test with Yates' continuity correction
0.7511235 0.3861208 1 Pearson's Chi-squared test with Yates' continuity correction
Made an attempt like this
library(broom)
Results<-for (i in 1:nrow(df)) {
assign(tidy(chisq.test(matrix(unlist(df[1,]), ncol = 2))))
}
Credit to: akrun for previous help