2

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?

luciano
  • 13,158
  • 36
  • 90
  • 130

2 Answers2

1

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
> 
rnso
  • 23,686
  • 25
  • 112
  • 234
-1

use this:

df <- as.table(rbind(c(3,0,5,5),c(0,0,0,2)))
> df
  A B C D
A 3 0 5 5
B 0 0 0 2
> chisq.test(df)

    Pearson's Chi-squared test

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

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

the result got warning maybe because of your data containing zero.

Datium
  • 93
  • 1
  • 7
  • Pleas comment houd answer Some more. Exploit what THE poster code thus and Clarice tour remraketten Aboutaleb "warning" – Kris Aug 17 '14 at 16:05