矩阵 R 上的下标数不正确
Posted
技术标签:
【中文标题】矩阵 R 上的下标数不正确【英文标题】:incorrect number of subscripts on matrix R 【发布时间】:2016-04-27 01:02:49 【问题描述】:虽然其他帖子也有同样的问题,但我无法使用该解决方案。我正在尝试生成一个矩阵来估计仅重要值的相关性。它应该很简单,但我收到错误“mat[i, j] 名称 样品 1 样品 2 样品 3 样品 4 样品 5 样品 6 样品 7 样品 8 样品 9 样品 10 Lrriq3 8.185794 5.691456 5.693373333 6.973468667 8.868912 5.915211333 6.718336667 6.212762667 6.424637333 13.01974667 DNA酶2b 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 优傲 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 斯帕塔1 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 输入:
名称 样品 1 样品 2 样品 3 样品 4 样品 5 样品 6 样品 7 样品 8 样品 9 样品 10 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 的排列顺序相同),并输出一个带有 p 值、估计值和测试的 Excel 表,以及一个仅对 p
脚本是:
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和编程的初学者。因此,如果有其他优化上述脚本的建议,请告诉我。
【问题讨论】:
你非常接近工作,所以我赞成平衡 也许是因为不够清晰?什么“其他帖子”?您还没有真正说出正确的答案是什么。你期待多少行? 45还是10?您仅在具有匹配行号的行之间寻求数字列的双向相关性?我读了三遍才知道这可能是目标。或者d<-NULL
不会创建空数据框这一事实。
@42 回答您的问题:帖子与论坛上的帖子一样。我清楚地说我期待一个以估计值作为值的矩阵。阅读以“脚本旨在进行相关性......只有那些 p
@HubertL 谢谢你-
您要求猜测投反对票的原因(然后您删除了该评论)。当人们响应您的请求时不要抱怨。
【参考方案1】:
当你写 result["estimate"]
时,你会得到一个列表,而如果你写 result[["estimate"]]
,你会得到一个数字。只需使用:
mat[i,j] <- result[["estimate"]]
你不会得到错误
【讨论】:
你有什么建议可以优化上面的代码或者是否足够好? @42 是真的,我不知道你在做什么,我只是发现了一个简单的错误,但我无法帮助优化,因为我不想读三遍以上是关于矩阵 R 上的下标数不正确的主要内容,如果未能解决你的问题,请参考以下文章
R语言创建使用矩阵(按行按列填充,矩阵命名,矩阵下标使用,数据框转换为矩阵)