“名称”属性的长度必须与ExpDes包中的向量的长度相同

时间:2019-06-25 13:45:00

标签: r vector

我正在尝试使用ExpDes软件包进行分析,但是由于出现错误,该函数无法运行:

Error in names(x) <- value : 'names' attribute [5] must be the same length as the vector [3]

    library(ExpDes)

temperature <- c(20,20,20,20,30,30,30,30,40,40,40,40)
    chemicals <- c(0,10,20,30,0,10,20,30,0,10,20,30)
    contaminant_A <- c(9,22,25,28,35,50,57,78,86,94,97,100)
    contaminant_B <- c(0, 0, 3, 3, 4, 5,11,11,13,18,18,21)
    contaminant_C <- c(68,57,53,50,44,38,36,34,31,29,25,24)
    contaminant_D <- c(39,68,35,32,53,69,69,60,35,30,43,36)

    DF <- data.frame(temperature,
                 chemicals,
                 contaminant_A,
                 contaminant_B,
                 contaminant_C,
                 contaminant_D)

    attach(DF)
    fat2.crd(temperature, chemicals, contaminant_A, quali = c(FALSE, FALSE), mcomp = "tukey",
         fac.names = c("Temperature","Chemicals"), sigT = 0.05, sigF = 0.05)

1 个答案:

答案 0 :(得分:0)

如果您在控制台中输入fat2.crd,则可以看到该函数的源代码。因为它们将aov应用于传递的因数和响应变量。在此之前,他们将因子变量转换为factor类,然后取其中的summary并更改列名。总之,他们正在尝试做的是

a1 <- summary(aov(DF$contaminant_A~factor(DF$temperature) * factor(DF$chemicals)))
a1

#                                            Df Sum Sq Mean Sq
#factor(DF$temperature)                       2  10749    5375
#factor(DF$chemicals)                         3    998     333
#factor(DF$temperature):factor(DF$chemicals)  6    279      47

然后做

colnames(a1[[1]]) <- c("DF", "SS", "MS", "Fc", "Pr>Fc")

给出

  

名称(x)<-值错误:     “名称”属性[5]的长度必须与矢量[3]的长度相同

因为只有3列,并且该函数传递5个名称进行重命名。

我们可以编写我们自己的函数版本,在此我们不转换传递的因子变量的类并照原样使用

a1 <- summary(aov(DF$contaminant_A~DF$temperature * DF$chemicals))
a1
#                            Df Sum Sq Mean Sq F value   Pr(>F)    
#DF$temperature               1  10731   10731  267.04 1.98e-07 ***
#DF$chemicals                 1    968     968   24.09  0.00118 ** 
#DF$temperature:DF$chemicals  1      6       6    0.14  0.71803    
#Residuals                    8    321      40                     
#---
#Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1ctor`

然后更改列名

colnames(a1[[1]]) <- c("DF", "SS", "MS", "Fc", "Pr>Fc")

该命令运行时没有错误,但是我不确定它在分析中可能会有什么影响。