r中循环中的可变长度错误

时间:2017-07-03 12:49:07

标签: r variables anova

我试图在r中循环以获得多个Anova结果。但是我一直收到以下错误:

Error in model.frame.default(formula = col ~ Total, data = phil_data, 
:variable lengths differ (found for 'Total')

我不知道Anova在循环外工作的地方(参见代码)。非常感谢任何帮助。

multi.hist(phil_data[,c(7:23)],density=TRUE,freq=TRUE,bcol="cyan",main=" ")
anovaresult <- anova(aov(x2 ~ Total, data=phil_data))
print(anovaresult)
anovaresult <- anova(aov(x1 ~ Total, data=phil_data))
print(anovaresult)

for(i in c(3, 7:23)){
    col <- colnames(phil_data)[i]
    print(col)
    anovaresult <- anova(aov(col~Total,data=phil_data))
    print(anovaresult)
}

1 个答案:

答案 0 :(得分:1)

    multi.hist(phil_data[,c(7:23)],density=TRUE,freq=TRUE,bcol="cyan",main=" ")
anovaresult <- anova(aov(x2 ~ Total, data=phil_data))
print(anovaresult)
anovaresult <- anova(aov(x1 ~ Total, data=phil_data))
print(anovaresult)

for(i in c(3, 7:23)){
    col[i] <- colnames(phil_data)[i]## add the i index
    print(col[i])
    anovaresult <- anova(aov(phil_data[,col[i]]~Total,data=phil_data))
    print(anovaresult)
}