0

我正在尝试遍历列并使用以下代码计算以下数据集的卡方检验

result=numeric(length(client-1))
for (i in 1:length(client-1)) {
result=chisq.test(table(client[[i]], client[[i+1]]))
}

我的数据集样本在这里

       sex                mar_st                                    education
1 Female         c) Cohabiting           e) Secondary form 3 to form 4
2 Female a) Married monogamous                       c) Primary 5 to 8
3 Female         c) Cohabiting                       c) Primary 5 to 8
4   Male a) Married monogamous         a) None (never attended school)
5   Male a) Married monogamous           e) Secondary form 3 to form 4
6 Female             d) Single f) Higher/tertiary (college/university)
7   Male             d) Single f) Higher/tertiary (college/university)
8 Female a) Married monogamous           e) Secondary form 3 to form 4
                work                   Q6
1  d) Skilled manual                     
2      h) Unemployed                     
3  d) Skilled manual                     
4     g) Agriculture                     
5 i) Other (specify) Business man        
6      h) Unemployed                     
7      h) Unemployed                     
8      h) Unemployed  

我收到以下错误/警告...

 #Error in .subset2(x, i, exact = exact) : subscript out of bounds
 #In addition: Warning messages:
 #1: In Ops.factor(left, right) : - not meaningful for factors

有人可以帮忙吗

4

1 回答 1

1

在我看来,这就是你想要做的:

result=numeric(length(client)-1)
for (i in 1:(length(client)-1)) {
  chi <- chisq.test(table(client[[i]], client[[i+1]]))
  result[i] <- chi[["statistic"]]
}
result
于 2013-10-28T08:47:47.867 回答