2016-01-20 223 views
1

虽然其他帖子也有同样的问题,但我无法使用该解决方案。我正在尝试生成一个仅用于重要值的相关估计矩阵。它应该是简单的,但我得到一个错误“在垫错误[I,J] < - 结果[”估计“]: 矩阵标数量不正确” 这是我的基因输入:矩阵R上的下标数量不正确

 
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片与p值时,估计,以及测试,以及只有那些具有p0.< 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语句或如果我只是打印出结果[“估计”],我不明白的错误。否则,我会遇到错误。

我是R和编程的初学者。因此,如果还有其他建议来优化上面的脚本,请告诉我。

+0

你非常接近一些工作,所以我upvoted为缺乏透明度或许平衡 – HubertL

+1

?什么“其他职位”?你还没有真正说过一个正确的答案。你有多少行? 45或10?只在具有匹配行号的行之间寻求数字列的双向相关性?拿了三个读书来获得这可能是目标。或者'd <-NULL'不会创建一个空的数据框。 –

+0

@ 42回复您的问题:帖子在字面上在论坛上的帖子。我清楚地表示,我期待一个估计值为矩阵的矩阵。阅读以“脚本旨在进行相关... ......只有那些p <0.05”为开头的部分。我不确定这些#行是指什么。它就像脚本中指出的那样(inc)的乌鸦。 – BioProgram

回答

1

当你写result["estimate"]你会得到一个列表,而如果你写result[["estimate"]]你会得到一个数字。只需使用:

mat[i,j] <- result[["estimate"]] 

,你不会得到错误

+0

你有任何建议来优化上面的代码或者是否足够好? – BioProgram

+1

@ 42是真的,我不明白你想要做什么,我只是发现了一个简单的错误,但我不禁要优化,因为我不想三次阅读 – HubertL

+1

够公平的。 – BioProgram