矩阵 R 上的下标数量不正确
incorrect number of subscripts on matrix R
虽然其他帖子也有同样的问题,但我无法使用该解决方案。我正在尝试生成一个矩阵来估计重要值的相关性。它应该很简单,但我收到错误 "Error in mat[i, j] <- result["estimate"] :
矩阵上的下标数量不正确”
这是我的 GENE 输入:
Name Sample1 Sample2 Sample3 Sample4 Sample5 Sample6 Sample7 Sample8 Sample9 Sample10
Lrriq3 8.185794 5.691456 5.693373333 6.973468667 8.868912 5.915211333 6.718336667 6.212762667 6.424637333 13.01974667
Dnase2b 0 0.1749128 0 0.1685122 0.1784736 0.122940127 0.007396118 0 0 0.09347276
Lphn2 1.080010133 10.01754067 14.10849333 11.77894 1.2552028 1.702124667 11.52506 15.21622 0.093035673 0.019666988
Rpf1 7.439926667 8.863518 10.28811467 11.86218 13.45304667 13.44146667 20.04024 16.94706667 23.76358 17.00742667
Uox 7.458356667 10.01754067 14.10849333 11.77894 19.75814 12.14829333 14.58846667 11.52506 15.21622 14.57954
Ctbs 0.400568 0.134638993 3.450422667 0.164317553 0 0 0.3395462 0.079734033 0.2700658 0
Spata1 2.066878 2.079750667 1.7238 2.240882667 1.461403333 2.093744 1.67564 1.2552028 1.702124667 1.427768
Ptprh 1.080010133 0.09089988 0.621011133 0.3004404 0.228991467 0.063827739 0.188904267 0.093035673 0.256751333 0.424108067
我的 LNC 输入:
Name Sample1 Sample2 Sample3 Sample4 Sample5 Sample6 Sample7 Sample8 Sample9 Sample10
XX1 3.956263333 2.443864667 1.413482 1.486519333 2.20473 3.015326 1.1033612 0.977534 0.789298267 1.469496
XX2 2.759029333 2.371987333 3.434 4.004905333 5.198814667 2.889342 3.463316 4.039935333 5.038084667 5.113266667
XX3 4.214811333 3.470377333 8.075684667 5.115368 7.084812667 4.767865333 6.272181333 6.202424667 5.480058667 4.613682
XX4 3.256852667 2.944397333 2.047966 1.696964667 2.099414667 1.780854667 0.3989612 0.23245 0.257986867 1.676498
XX5 661.7403333 647.749 834.8288 670.8856 728.8326667 710.5224667 357.7705333 387.9334 404.3672667 694.4849333
XX6 7.458356667 10.01754067 14.10849333 11.77894 11.77894 19.75814 11.77894 1.2552028 1.702124667 11.52506
XX7 7.458356667 10.01754067 14.10849333 11.77894 19.75814 14.58846667 11.52506 13.45304667 13.44146667 0.23245
脚本旨在对每个文件的每一行进行关联(注意样本 1 到 10 的排列顺序相同)并输出一个 excel sheet 和 p 值,估计和检验,以及仅 p < 0.05 的那些的估计矩阵。除了一步之外,所有脚本都有效。
脚本是:
genes <- read.delim(file="SampleGene.txt", header=TRUE, row.names=1)
lnc <- read.delim(file="Samplelncs.txt", header=TRUE, row.names=1)
x = rownames(genes[1:nrow(genes),])
y = rownames(lnc[1:nrow(lnc),])
d<-NULL #creates an empty dataframe
mat<-matrix(0,nrow(genes),nrow(lnc)) #creates a matrix with all values as 0
rownames(mat) <- rownames(genes) #assigns rownames to the matrix based on row names of the gene file
colnames(mat) <- rownames(lnc) #assigns colnames to the matrix based on the colnames of the lnc file
for (i in x){
for (j in y) {
result=cor.test(as.numeric(genes[i,]), as.numeric(lnc[j,]), method='pearson')#calculates the correlation and assigns it to result
d<-rbind(d, data.frame(i, j, result[c("estimate","p.value","statistic","method")], stringsAsFactors=FALSE)) #rbind allows writing output of loop to an empty dataframe. Works perfectly.
if (result["p.value"]<0.05){ #attempts to add the estimate to the matrix only of p.value <0.05
mat[i,j] <- result["estimate"] #This is causing the error
#print(result["estimate"]) #if I just print without adding to matrix, i dont get errors
}
}
}
write.table(file="Pearson.xls", as.data.frame(d), sep="\t")
正如我指出的那样,如果我从循环中删除 if 语句或者如果我只是打印出结果 ["estimate"],我不会收到错误。否则,我总是出错。
我是R初学者programming.Hence,如果有其他优化上面脚本的建议,请告诉我。
当你写 result["estimate"]
时你会得到一个列表,而如果你写 result[["estimate"]]
你会得到一个数字。只需使用:
mat[i,j] <- result[["estimate"]]
你不会得到错误
虽然其他帖子也有同样的问题,但我无法使用该解决方案。我正在尝试生成一个矩阵来估计重要值的相关性。它应该很简单,但我收到错误 "Error in mat[i, j] <- result["estimate"] : 矩阵上的下标数量不正确” 这是我的 GENE 输入:
Name Sample1 Sample2 Sample3 Sample4 Sample5 Sample6 Sample7 Sample8 Sample9 Sample10 Lrriq3 8.185794 5.691456 5.693373333 6.973468667 8.868912 5.915211333 6.718336667 6.212762667 6.424637333 13.01974667 Dnase2b 0 0.1749128 0 0.1685122 0.1784736 0.122940127 0.007396118 0 0 0.09347276 Lphn2 1.080010133 10.01754067 14.10849333 11.77894 1.2552028 1.702124667 11.52506 15.21622 0.093035673 0.019666988 Rpf1 7.439926667 8.863518 10.28811467 11.86218 13.45304667 13.44146667 20.04024 16.94706667 23.76358 17.00742667 Uox 7.458356667 10.01754067 14.10849333 11.77894 19.75814 12.14829333 14.58846667 11.52506 15.21622 14.57954 Ctbs 0.400568 0.134638993 3.450422667 0.164317553 0 0 0.3395462 0.079734033 0.2700658 0 Spata1 2.066878 2.079750667 1.7238 2.240882667 1.461403333 2.093744 1.67564 1.2552028 1.702124667 1.427768 Ptprh 1.080010133 0.09089988 0.621011133 0.3004404 0.228991467 0.063827739 0.188904267 0.093035673 0.256751333 0.424108067
我的 LNC 输入:
Name Sample1 Sample2 Sample3 Sample4 Sample5 Sample6 Sample7 Sample8 Sample9 Sample10 XX1 3.956263333 2.443864667 1.413482 1.486519333 2.20473 3.015326 1.1033612 0.977534 0.789298267 1.469496 XX2 2.759029333 2.371987333 3.434 4.004905333 5.198814667 2.889342 3.463316 4.039935333 5.038084667 5.113266667 XX3 4.214811333 3.470377333 8.075684667 5.115368 7.084812667 4.767865333 6.272181333 6.202424667 5.480058667 4.613682 XX4 3.256852667 2.944397333 2.047966 1.696964667 2.099414667 1.780854667 0.3989612 0.23245 0.257986867 1.676498 XX5 661.7403333 647.749 834.8288 670.8856 728.8326667 710.5224667 357.7705333 387.9334 404.3672667 694.4849333 XX6 7.458356667 10.01754067 14.10849333 11.77894 11.77894 19.75814 11.77894 1.2552028 1.702124667 11.52506 XX7 7.458356667 10.01754067 14.10849333 11.77894 19.75814 14.58846667 11.52506 13.45304667 13.44146667 0.23245
脚本旨在对每个文件的每一行进行关联(注意样本 1 到 10 的排列顺序相同)并输出一个 excel sheet 和 p 值,估计和检验,以及仅 p < 0.05 的那些的估计矩阵。除了一步之外,所有脚本都有效。
脚本是:
genes <- read.delim(file="SampleGene.txt", header=TRUE, row.names=1)
lnc <- read.delim(file="Samplelncs.txt", header=TRUE, row.names=1)
x = rownames(genes[1:nrow(genes),])
y = rownames(lnc[1:nrow(lnc),])
d<-NULL #creates an empty dataframe
mat<-matrix(0,nrow(genes),nrow(lnc)) #creates a matrix with all values as 0
rownames(mat) <- rownames(genes) #assigns rownames to the matrix based on row names of the gene file
colnames(mat) <- rownames(lnc) #assigns colnames to the matrix based on the colnames of the lnc file
for (i in x){
for (j in y) {
result=cor.test(as.numeric(genes[i,]), as.numeric(lnc[j,]), method='pearson')#calculates the correlation and assigns it to result
d<-rbind(d, data.frame(i, j, result[c("estimate","p.value","statistic","method")], stringsAsFactors=FALSE)) #rbind allows writing output of loop to an empty dataframe. Works perfectly.
if (result["p.value"]<0.05){ #attempts to add the estimate to the matrix only of p.value <0.05
mat[i,j] <- result["estimate"] #This is causing the error
#print(result["estimate"]) #if I just print without adding to matrix, i dont get errors
}
}
}
write.table(file="Pearson.xls", as.data.frame(d), sep="\t")
正如我指出的那样,如果我从循环中删除 if 语句或者如果我只是打印出结果 ["estimate"],我不会收到错误。否则,我总是出错。
我是R初学者programming.Hence,如果有其他优化上面脚本的建议,请告诉我。
当你写 result["estimate"]
时你会得到一个列表,而如果你写 result[["estimate"]]
你会得到一个数字。只需使用:
mat[i,j] <- result[["estimate"]]
你不会得到错误