I want to compare two groups (vectors) having different lengths to see if their distributions differ considerably. However, when I use chisq.test, I get an error due to inequalities of lengths:

> chisq.test(factor(Group1), factor(Group2))

results in:

Error in chisq.test(factor(Group1[, col]), factor(Group2[, col])) : 
'x' and 'y' must have the same length

Could someone help me with this? Thanks.

  • 2
    $\begingroup$ This seems an odd thing to be doing. Can you forget the code and more clearly explain what you're trying to do, in specific terms, and why you're trying to do it? What's in Group1 and Group2? $\endgroup$
    – Glen_b
    Feb 18, 2014 at 3:22
  • $\begingroup$ @Glen_b, I am working on a very large dataset of patients who belong to one of five groups. I want to test if groups are not so much different than each other in terms of age, race, sex, smoking level, weight, and some other factors. Some factors are numerical and some are categorical. I used ANOVA to compare mean differences between groups for numerical factors. Also for categorical factors, I want to use chi-squared test to compare groups' distributions. However, I do not know how to do chi-sq test in R when the two vectors have different lengths. $\endgroup$ Feb 18, 2014 at 3:30
  • $\begingroup$ Cont- Assume that the vectors are the values of "race" column for different groups. $\endgroup$ Feb 18, 2014 at 3:34
  • 2
    $\begingroup$ Surely 'not so much different' is a question about effect size (how different are they?) not hypothesis test ("is our sample large enough to pick up even unimportant differences?"). Why a hypothesis test? $\endgroup$
    – Glen_b
    Feb 18, 2014 at 3:37

1 Answer 1


I think (as mentioned in comments) that a hypothesis test doesn't really answer the question you say you're interested in. (It also lacks power if any of the factors are ordered).

The thing is, 'not so much different' relates to a question about effect size (how different are they?) not a hypothesis test ("is our sample large enough to pick up even unimportant differences?").

You say you have very large sample sizes. This will cause you to reject as different distributions that are quite similar (since you'll have enough power to pick up tiny differences). Is that really what you mean to do? Or would you rather be able to say 'actually, they're fairly similarly distributed' when that's the case?

The direct answer to the question is you use table on the pair of factors you want to test (e.g. Group and race) and then use that as input to chisq.test e.g.

# make up some data
mydata <- data.frame(group=as.factor(sample(1:5,199,replace=TRUE)),

# look at the table:
 (mytab <- with(mydata,table(group,race)) )
group  1  2  3
    1 16 19 11
    2 14 15 13
    3  9 14 20
    4 12 13 11
    5 11 11 10

(you'll note that each group is a different size - e.g. group 1 has 46 people, group 5 has 32)

 # do the chi-square 

    Pearson's Chi-squared test

data:  mytab
X-squared = 6.0928, df = 8, p-value = 0.6368

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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