R中的这个非数字矩阵范围错误是啥?
Posted
技术标签:
【中文标题】R中的这个非数字矩阵范围错误是啥?【英文标题】:What is this non numeric matrix extent error in R?R中的这个非数字矩阵范围错误是什么? 【发布时间】:2020-07-16 21:20:11 【问题描述】:我试图将一个函数应用到我的列表中,但它返回此错误
“非数值矩阵范围误差”
这是我的代码
错误发生在最后几行 代码到最后都可以正常工作,因此,我无法绘制我的图表 我在网上搜索过,但找不到任何有用的东西,我看不出代码有什么问题
#Question 1
set.seed(10000)
v <- c(0.1,0.5,1,2,5,10,100)
lyst <- list()
for(i in v)
for(j in v)
elementname <- paste0(as.character(i),"-",as.character(j))
print(elementname)
lyst[[elementname]] <- rgamma(10000,i,j)
#Question 2
pdf("Question2.pdf",width = 20, height = 10)
par(mfcol=c(7,7))
for(x in names(lyst))
hist(lyst[[x]],
xlab = "Value",
main = paste("Alpha-Lambda:",x))
dev.off()
#Question 3
theoretical_mean <- matrix(ncol=7,nrow=7,dimnames=list(as.character(v), as.character(v)))
theoretical_var <- matrix(ncol=7,nrow=7,dimnames=list(as.character(v), as.character(v)))
for (i in 1:7)
for (j in 1:7)
theoretical_mean[j,i] <- as.character(v[i]/v[j])
theoretical_var[j,i] <- as.character(v[i]/(v[j]^2))
sample_mean <-lapply(lyst, mean)
sample_mean <- as.data.frame(matrix(unlist(sample_mean),nrow = 7, ncol = 7, byrow = T))
sample_mean <- round(sample_mean,digits = 3)
sample_mean <- data.matrix(sample_mean, rownames.force = NA)
sample_var <-lapply(lyst, var)
sample_var <- as.data.frame(matrix(unlist(sample_var),nrow = 7, ncol = 7, byrow = T))
sample_var <- round(sample_var,digits = 3)
sample_var <- data.matrix(sample_var, rownames.force = NA)
theor_sample_mean <- matrix(paste(theoretical_mean, sample_mean, sep=" - "),nrow=7,dimnames = dimnames(theoretical_var))
theor_sample_var <- matrix(paste(theoretical_var, sample_var, sep=" - "),nrow=7,dimnames= dimnames(theoretical_var))
sink("Q3.txt")
cat("Theoretical Mean vs. Sample Mean:\n")
print(as.table(theor_sample_mean))
cat("\n")
cat("Theoretical Variance vs. Sample Variance:\n")
print(as.table(theor_sample_var))
sink()
#Question 4
nmean <- function(x)
m <- matrix(nrow=nrow(x))
for (j in 1:ncol(x))
v <- c()
for(i in 1:nrow(x))
v <- c(v,mean(x[1:i,j]))
m <- cbind(m,v)
m <- m[,-1]
colnames(m) <- colnames(x)
rownames(m) <- NULL
return(m)
sequentialMeans <- lapply(lyst,nmean)
pdf("Question4.pdf",width=15,height=10)
for (i in 1:7)
for (j in 1:7)
plot(y=sequentialMeans[[i]][,j],x=1:10000,xlab="n value",ylab="Values", main=paste("Alpha-Lambda:",colnames(lyst[[i]])[j]),type="l")
dev.off()
【问题讨论】:
【参考方案1】:你的代码的问题是nmean
函数的输入数据格式根据行
nmean <- function(x)
m <- matrix(nrow=nrow(x))
for (j in 1:ncol(x))
v <- c()
for(i in 1:nrow(x))
v <- c(v,mean(x[1:i,j]))
m <- cbind(m,v)
m <- m[,-1]
colnames(m) <- colnames(x)
rownames(m) <- NULL
return(m)
是一个矩阵,您希望向它提供伽马分布值的向量,如下行中指定的那样
lyst <- list()
for(i in v)
for(j in v)
elementname <- paste0(as.character(i),"-",as.character(j))
print(elementname)
lyst[[elementname]] <- rgamma(10000,i,j)
对于具有向量类型的x
,函数ncol(x)
和nrow(x)
return NULL
。此外,ncol(x)
也不能申请。
如果您想保存您的方法,您需要考虑将数据转换为矩阵格式,或者使用矢量格式,但使用矢量兼容函数 length(x)
来获取向量的长度,names(lyst)
来获取向量的长度名字。
更新:
cmets 中的代码有效,但您必须更改lapply
-statement,因为您现在有一个矩阵,您可以直接将其用作nmean
函数的输入。以下代码用于生成 sampleMeans
并避免您问题的原始错误消息。为了减少运行时间,它只需要 100 个样本。
#Question 1
set.seed(10000)
v <- c(0.1,0.5,1,2,5,10,100)
lyst <- list()
for(i in v)
for(j in v)
elementname <- paste0(as.character(i),"-",as.character(j))
print(elementname)
lyst[[elementname]] <- rgamma(100,i,j)
#Question 2
pdf("Question2.pdf",width = 20, height = 10)
par(mfcol=c(7,7))
for(x in names(lyst))
hist(lyst[[x]],
xlab = "Value",
main = paste("Alpha-Lambda:",x))
dev.off()
#Question 3
theoretical_mean <- matrix(ncol=7,nrow=7,dimnames=list(as.character(v), as.character(v)))
theoretical_var <- matrix(ncol=7,nrow=7,dimnames=list(as.character(v), as.character(v)))
for (i in 1:7)
for (j in 1:7)
theoretical_mean[j,i] <- as.character(v[i]/v[j])
theoretical_var[j,i] <- as.character(v[i]/(v[j]^2))
sample_mean <-lapply(lyst, mean)
sample_mean <- as.data.frame(matrix(unlist(sample_mean),nrow = 7, ncol = 7, byrow = T))
sample_mean <- round(sample_mean,digits = 3)
sample_mean <- data.matrix(sample_mean, rownames.force = NA)
sample_var <-lapply(lyst, var)
sample_var <- as.data.frame(matrix(unlist(sample_var),nrow = 7, ncol = 7, byrow = T))
sample_var <- round(sample_var,digits = 3)
sample_var <- data.matrix(sample_var, rownames.force = NA)
theor_sample_mean <- matrix(paste(theoretical_mean, sample_mean, sep=" - "),nrow=7,dimnames = dimnames(theoretical_var))
theor_sample_var <- matrix(paste(theoretical_var, sample_var, sep=" - "),nrow=7,dimnames= dimnames(theoretical_var))
sink("Q3.txt")
cat("Theoretical Mean vs. Sample Mean:\n")
print(as.table(theor_sample_mean))
cat("\n")
cat("Theoretical Variance vs. Sample Variance:\n")
print(as.table(theor_sample_var))
sink()
lyst = matrix(unlist(lyst), ncol = 7, byrow = TRUE)
colnames(lyst) = c("100-0.1","100-0.5","100-1","100-2","100-5","100-10","100-100")
#Question 4
nmean <- function(x)
m <- matrix(nrow=nrow(x))
for (j in 1:ncol(x))
v <- c()
for(i in 1:nrow(x))
v <- c(v,mean(x[1:i,j]))
m <- cbind(m,v)
m <- m[,-1]
colnames(m) <- colnames(x)
rownames(m) <- NULL
return(m)
sequentialMeans <- nmean(lyst)
还要注意,您需要调整 Q4 的代码,即绘图生成。以下代码有效。
pdf("Question4.pdf",width=15,height=10)
for (i in 1:7)
for (j in 1:7)
plot(y=sequentialMeans[,j],x=1:700,xlab="n value",ylab="Values", main=paste("Alpha-Lambda:",colnames(lyst[,j]),type="l"))
dev.off()
如果这有帮助,请告诉我。
【讨论】:
类似lyst <- matrix(unlist(lyst), ncol = 7, byrow = T)
是的,这是一种可能性。但请注意,您仍需要事先提供 colnames(lyst)
才能使您的功能正常工作!
嗯,我在colnames(lyst) <- c("100-0.1","100-0.5","100-1","100-2","100-5","100-10","100-100")
中添加了但它仍然为我返回了同样的错误
@user12197328 我更新了答案以反映您的评论。
感谢您的大力回复,感谢您的帮助,但它确实修复了我收到的错误,但现在它给了我另一个错误Error in xy.coords(x, y, xlabel, ylabel, log) : object 'sequentialMeans' not found
以上是关于R中的这个非数字矩阵范围错误是啥?的主要内容,如果未能解决你的问题,请参考以下文章