1
votes

I have a dataframe, and want to perform for each row (3x2 contingency table) a chi squared test .

  • row 1 102 4998 105 3264 105 3636
  • row 2 210 4890 22 3347 20 3721
  • row 3 ...

So for the first row a chi squared test should be performed for the following contingency table;

  • group A 102 4998
  • group B 105 3264
  • group C 105 3636

I use the following code, but this does not calculate the correct p-value (all p-values are equal to zero while this is not the case when I calculate the chi-square test myself):

table <- read.delim("dataframe.txt")
apply(table, 1, function(x) chisq.test(matrix(x,nrow=3)))

Could anyone help me with this?

Thank you in advance

Wannes

1
Are you confused about the row x col convention in R? Or are you confused about the fact that converting vectors to matrices assumes a column-major cosntructuion?IRTFM

1 Answers

1
votes

Use ncol instead of nrow:

apply(table, 1, function(x) chisq.test(matrix(x,ncol=3)))

Currently, you are building a matrix that looks like this for the first row:

102 3264
4998 105
105 3636

When R builds matrices from vectors, it builds them column-wise, so the second value goes into the second row, etc.

Also, if you want it to report just the p-value, you can do the following:

apply(table, 1, function(x) chisq.test(matrix(x,ncol=3))$p.value)