I have to perform a chi-square test on this given data set:


Upon doing so with the following code:

I get the following result:

        Pearson's Chi-squared test

data:  data.table 
X-squared = NaN, df = 44, p-value = NA

Warning message:
In chisq.test(data.table) : Chi-squared approximation may be incorrect

Why is the X-squared value "NaN" and the p-value "N/A"? What am I doing wrong here?

  • $\begingroup$ Could you tell us what units of measurement your numbers are in? $\endgroup$
    – whuber
    Mar 27, 2013 at 13:20
  • $\begingroup$ Are the units of measurement even relevant? The data that I am performing the Chi-Square test upon, is composed of the monthly rainfall average(in cms) of a particular location, for 5 years:2008,2009,2010,2011,2012. $\endgroup$
    – TGK
    Mar 27, 2013 at 14:14
  • 5
    $\begingroup$ They are as relevant as anything else, because--among other things--the chi-squared test is applied to counts. So let's go back to the beginning, since obviously the chi-squared test is wrong here: what are you trying to learn about these data? $\endgroup$
    – whuber
    Mar 27, 2013 at 14:16
  • 2
    $\begingroup$ If you type ?chisq.test you will see that, if (as in your case) x is a vector then the values in it must be non negative integers $\endgroup$
    – Peter Flom
    Mar 27, 2013 at 18:48
  • 8
    $\begingroup$ It simply makes no sense whatever to apply a chi-square test to these measurements. The technical reason for getting NAs is because for the months March, July and December the expecteds you're generating are all zero. Their contribution to chi-square is a sum of terms like $(0 - 0)^2/0$... but they're meaningless anyway, so it's actually a good thing the analysis doesn't work; better no answer than a meaningless one that appears meaningful - that's dangerous. Whoever has asked you to do such a thing apparently has no idea what they're doing. $\endgroup$
    – Glen_b
    Mar 27, 2013 at 23:08

1 Answer 1


As Glen_B pointed out, your problem is that you can't have expected frequencies to be zero, because that would throw off the calculation of the chi-squared statistic (you'd have zeros in the denominator). For example, see that

x1 <- matrix(c(0, 1, 0, 1), 2, 2)
     [,1] [,2]
[1,]    0    0
[2,]    1    1


x2 <- matrix(c(0, 0, 1, 1), 2, 2)
     [,1] [,2]
[1,]    0    1
[2,]    0    1

will both fail to produce chi-squared tests (as they should), whereas

x3 <- matrix(c(0, 1, 0, 1), 2, 2)
     [,1] [,2]
[1,]    0    1
[2,]    1    0

will give you no problems.

BTW, this issue was also addressed here and here.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.