0

我有以下数据框,我称之为“测试”,我正在尝试为每个“元”与“诊断”运行 Bartlett 测试和 Kruskal-Wallis 测试

> test

Index   tube.label  age gender  diagnosis   metab1  metab2  metab3  metab4  metab5  metab6
1            200    73  Male    Cancer         6    1.5         2      5       8    1.5
2            201    71  Male    Healthy        6    1.5         2    11.5     50    1.5
4            202    76  Male    Adenoma        2    1.5         2      5       8    1.5
7            203    58  Female  Cancer         2    1.5         2    1.5     2.5    1.5
9            204    73  Male    Cancer         2    1.5         2    1.5       8    1.5
12           205    72  Male    Healthy        6    1.5    17.8272  13.5    184.2   4.5
13           206    46  Female  Cancer     30.0530  1.5        2    21.2    16.6    4.5
14           207    38  Female  Healthy        6    1.5        2    12.494  31.59   1.5
15           208    60  Male    Cancer         6    1.5        2    13.2    53.2    4.5
16           209    72  Female  Cancer         6    1.5        2    1.5        8    1.5
17           210    72  Male    Adenoma        6    1.5        2    22.829  102.44  9.069
18           211    52  Male    Cancer         6    1.5        2    1.5        8    1.5
19           212    64  Male    Healthy        6    1.5        2    1.5        8    1.5
20           213    68  Male    Cancer         6    1.5        2    26.685  40.9    4.5
21           214    60  Male    Healthy    24.902   1.5   42.443    22.942  498.5   4.5
23           215    70  Female  Healthy         6   1.5        2    1.5     19.908  4.5
24           216    42  Female  Healthy         6   1.5        2    1.5      17.7   1.5
25           217    72  Male    Inflammation    6   1.5        2    1.5         8   1.5
26           218    71  Male    Healthy        51   1.5        2    41.062  182.2   11.340
27           219    51  Female  Inflammation    2   1.5        2    1.5         8   1.5

我可以单独运行它们,它给了我正确的值:

bartlett.test(metab1 ~ diagnosis, data = test)

    Bartlett test of homogeneity of variances

data:  metab1 by diagnosis
Bartlett's K-squared = 5.1526, df = 3, p-value = 0.161
kruskal.test(metab1 ~ diagnosis, data = test)

    Kruskal-Wallis rank sum test

data:  metab1 by diagnosis
Kruskal-Wallis chi-squared = 4.3475, df = 3, p-value = 0.2263

但是,当我尝试运行 for 循环(我有超过 100 个要运行)时,我不断收到以下错误:

巴特利特错误:

testcols <- colnames(test[6:ncol(test)])
for (met in testcols){
  bartlett.test(met ~ diagnosis, data = test)
}

>Error in model.frame.default(formula = met ~ diagnosis, data = test) : 
  variable lengths differ (found for 'diagnosis')

Kruskal-Wallis 错误:

for(met in testcols){
  kruskal.test(met ~ diagnosis,data = test)
}

>Error in model.frame.default(formula = met ~ diagnosis, data = test) : 
  variable lengths differ (found for 'diagnosis')

我应该使用别的东西吗?感谢您的帮助!

4

1 回答 1

3

尝试使用以下方法创建要应用的公式reformulate

cols <- names(test)[6:ncol(test)]

all_test <- lapply(cols, function(x) 
                    bartlett.test(reformulate("diagnosis", x), data = test))

您可以对kruskal.test.

于 2020-08-07T01:41:05.557 回答