I have a dataset of race and outcome either (Y,N) I want to tabulate a 2X2 table to run a chisq test for each race.
Asian 584 24
Black 1721 56
Hispanic 2400 90
White 8164 289
Once I create a table 2X2 so the first row will be Asian and second row will be non-Asian (counted from values of total - asianNo) and (total- asian yes) as the second column of that row. Then I can run a chisq test easily on each race once I repeat that process for all races. Is there an easier way to run a Chisq test for each race in my table above?
If d
is your data, then the below will produce your list of 2x2 tables
tables <- lapply(1:nrow(d), function(x) rbind(d[x,], colSums(d[-x,])))
names(tables) <- rownames(d)
You can then apply the chi-sq test to each
lapply(tables, chisq.test )
Input:
d = matrix(c(584,24,1721,56,2400,90,8164,289), nrow=4, byrow=T,dimnames = list(c("Asian", "Black", "Hispanic", "White")))