Chi Square Test in R

347 Views Asked by At

I have a dataset on Excel which has lots of columns (A, B, C...Z) and I want to perform a Chi-Square Test to obtain p value between B e C column only. Columns B and C only have two types of values: 0, 1. The column A on Excel corresponds to the identification number of the person. I proceeded as follows:

  1. I created a contingency table by running this code

    write.table(db,'my_data.txt',quote=FALSE,sep='\t', row.names=FALSE)

    file_path <- 'my_data.txt'

    dbtable <- read.delim(file_path, row.names=1)

  2. I created then a table containing only the columns I am interested in performing the Chi Square Test on, thus:

    db2columns <- dbtable %>% select(B, C)

  3. I tried to run the Chi Square Test on db2columns, as it follows: chisq <- chisq.test(db2columns) chisq However it displays: Error in chisq.test(db2columns) : all entries of 'x' must be nonnegative and finite I tried to convert db2columns to a contingency table but it doesn't work too, saying the same error.

I have then two questions

  1. Is there a faster way to perform chi square test on two columns only without all the steps I wrote above?
  2. How do I solve the error?

I am new to R so sorry if it's a simple question

Thank you!

0

There are 0 best solutions below