错误:'dimnames'[2]的长度不等于数组范围

时间:2015-09-08 18:29:49

标签: r

我已经改变了一些完美的R代码,但现在我有7个集群。

layout(matrix(c(1, 1, 2, 2, 3, 3, 4, 4, 5), ncol=1))

# main plots
par(mai=rep(0.5, 4))

fcm <-c(14.0,14.1,13.0,14.2,14.7,13.8,14.0)
gk  <-c(12.1,12.5,12.2,12.0,11.5,12.0,11.4)
gg  <-c(14.0,14.1,13.3,12.8,12.0,12.2,12.0)
data1 <- rbind(fcm,gk,gg)
colnames(data1) <- c(6,7,8,9,10,11,12)

fcm <-c(2.65,2.55,2.4,2.45,2.45,2.5,2.45)
gk  <-c(2.45,2.55,2.4,2.3,2.2,2.35,2.1)
gg  <-c(2.6,2.65,2.5,2.35,2.4,2.4,2.2)
data2 <- rbind(fcm,gk,gg)
colnames(data2) <- c(6,7,8,9,10,11,12)

fcm <-c(8.8,6.5,6.6,8.2,8.0,8.4,9.0)
gk  <-c(12.7,11.0,11.1,10.5,10.7,10.0,9.5)
gg  <-c(2.1,2.1,1.8,2.0,2.0,1.9,1.8)
data3 <- rbind(fcm,gk,gg)
colnames(data3) <- c(6,7,8,9,10,11,12)

fcm <-c(0.47,0.53,0.45,0.39,0.40,0.47,0.48)
gk  <-c(0.45,0.51,0.34,0.40,0.42,0.42,0.44)
data4 <- rbind(fcm,gk)
colnames(data4) <- c(6,7,8,9,10,11,12)

barplot(as.matrix(data1),ylim=c(0,20),main="P wave",
        xlab="number of clusters", ylab="traveltime rms(ms)",
        col=c("red", "black", "green"), beside=TRUE)
barplot(as.matrix(data2),ylim=c(0,2),main="MT",
        xlab="number of clusters", ylab="MT functions",
        col=c("red", "black", "green"), beside=TRUE)
barplot(as.matrix(data3),ylim=c(0,20),main="XBI",
        xlab="number of clusters", ylab="index value",
        col=c("red", "black", "green"), beside=TRUE)
barplot(as.matrix(data4),ylim=c(0,0.6),main="NCE",
        xlab="number of clusters", ylab="index value",
        col=c("red", "black"), beside=TRUE)

par(mai=c(0,0,0,0))
plot.new()
legend(legend = c("fcm","gk","gg"), fill = c( "red", "black", "green"), 
    "center", horiz=TRUE)

但后来我得到了

Error in `colnames<-`(`*tmp*`, value = c(6, 7, 8, 9, 10, 11, 12)) : 
  length of 'dimnames' [2] not equal to array extent

问题在于rbind。我正在组合3个向量,每个向量都有7个元素。我应该改变什么?我希望每个元素都有适当数量的标记在后面的簇。

1 个答案:

答案 0 :(得分:3)

您需要再提供一个列名。您的数据有八列,可以看到,例如

> length(fcm)
[1] 8

> ncol(data1)
[1] 8

或通过显示矩阵:

> data1
#    [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
#fcm 14.0 14.1 13.0   14  2.0 14.7 13.8 14.0
#gk  12.1 12.5 12.2   12  0.0 11.5 12.0 11.4
#gg  14.0 14.1 13.0    3 12.8 12.0 12.2 12.0

> length( c(6, 7, 8, 9, 10, 11, 12))
[1] 7

您可以尝试使用

colnames(data1) <- c(6:13)

根据您的数据,这将是:

fcm <-c(14.0,14.1,13.0,14,2,14.7,13.8,14.0)
gk  <-c(12.1,12.5,12.2,12,0,11.5,12.0,11.4)
gg  <-c(14.0,14.1,13,3,12.8,12.0,12.2,12.0)
data1 <- rbind(fcm,gk,gg)
colnames(data1) <- c(6:13)

给出:

> data1
       6    7    8  9   10   11   12   13
fcm 14.0 14.1 13.0 14  2.0 14.7 13.8 14.0
gk  12.1 12.5 12.2 12  0.0 11.5 12.0 11.4
gg  14.0 14.1 13.0  3 12.8 12.0 12.2 12.0

没有任何错误消息。