gpt4 book ai didi

r - 错误 : length of 'dimnames' [2] not equal to array extent

转载 作者:行者123 更新时间:2023-12-04 10:04:53 25 4
gpt4 key购买 nike

关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。












想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。

5年前关闭。




Improve this question




我已经对我的 R 代码进行了一些完美的更改,但是现在我有 7 个集群而不是 3。

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 个元素。我应该更改什么?我希望每个元素都有正确数量的后面标记的簇。

最佳答案

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

> 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

没有任何错误信息。

关于r - 错误 : length of 'dimnames' [2] not equal to array extent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32464829/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com