rchi-squared

chisq.test for each row on four numbers and output in new data frame in R


I have a data frame where each row contains numbers of a contingency table on which I would like to run a chisq.test command (to each row in data frame) in R. The output from each row should be added into the data frame as new columns (X-squared-value,p-value).

DF1:

ID1 ID2 female_boxing female_cycling male_boxing male_cycling 
A zit 43 170 159 710
B tag 37 134 165 744
C hfs 32 96 170 784
D prt 17 61 185 811
E its 31 112 169 762
F qrw 68 233 130 645

This is what I tried:

apply(DF1[,c('female_boxing','female_cycling','male_boxing','male_cycling')], 1, function(x) chisq.test(x) )

But this gives me only the summary table for each row.


Solution

  • You were close, just inspect one single test with str which helps you to decide which elements to select.

    apply(dat[,c('female_boxing','female_cycling','male_boxing','male_cycling')], 
          1, function(x) chisq.test(x)[c('statistic', 'p.value')] )
    

    The apply gives you a list, the results are a little nicer using sapply and looping over the rows.

    chi <- t(sapply(seq(nrow(dat)), function(i) 
      chisq.test(dat[i, c('female_boxing','female_cycling','male_boxing','male_cycling')])[
        c('statistic', 'p.value')]))
    
    cbind(dat, chi)
    #   ID1 ID2 female_boxing female_cycling male_boxing male_cycling statistic       p.value
    # 1   A zit            43            170         159          710  988.7209 5.033879e-214
    # 2   B tag            37            134         165          744  1142.541 2.146278e-247
    # 3   C hfs            32             96         170          784  1334.991 3.762222e-289
    # 4   D prt            17             61         185          811  1518.015             0
    # 5   E its            31            112         169          762  1245.218 1.133143e-269
    # 6   F qrw            68            233         130          645  752.3941 9.129485e-163
    

    Data:

    dat <- structure(list(ID1 = c("A", "B", "C", "D", "E", "F"), ID2 = c("zit", 
    "tag", "hfs", "prt", "its", "qrw"), female_boxing = c(43L, 37L, 
    32L, 17L, 31L, 68L), female_cycling = c(170L, 134L, 96L, 61L, 
    112L, 233L), male_boxing = c(159L, 165L, 170L, 185L, 169L, 130L
    ), male_cycling = c(710L, 744L, 784L, 811L, 762L, 645L)), class = "data.frame", row.names = c(NA, 
    -6L))