Run chi-squared test on a data.frame

luciano picture luciano · Aug 17, 2014 · Viewed 8.6k times · Source

I have this data.frame:

df <- data.frame(xy = c("x", "y"), V1  = c(3, 0), V2 = c(0, 0), V3 = c(5, 0), V4 = c(5, 2))
df 
  xy V1 V2 V3 V4
1  x  3  0  5  5
2  y  0  0  0  2

I want to know if x or y is more associated with any of V1, V2, V3 or V4. To test this, I can use a chi-squared.

This is what I've tried, none of which work:

chisq.test(df)
chisq.test(as.matrix(df))
chisq.test(as.table(df))

How can I run a chi-squared test on df?

Answer

rnso picture rnso · Aug 17, 2014

Both of following work (you need to remove first column):

chisq.test(df[,-1])
chisq.test(as.matrix(df[,-1]))

> chisq.test(df[,-1])

        Pearson's Chi-squared test

data:  df[, -1] 
X-squared = NaN, df = 3, p-value = NA

Warning message:
In chisq.test(df[, -1]) : Chi-squared approximation may be incorrect
> 
> 
> 
> 
> 
> chisq.test(as.matrix(df[,-1]))

        Pearson's Chi-squared test

data:  as.matrix(df[, -1]) 
X-squared = NaN, df = 3, p-value = NA

Warning message:
In chisq.test(as.matrix(df[, -1])) :
  Chi-squared approximation may be incorrect
>