矩阵R上的下标数量不正确

时间:2016-01-20 23:29:14

标签: r loops matrix correlation

虽然其他帖子也有同样的问题,但我无法使用该解决方案。我正在尝试生成一个矩阵,用于仅估计有效值的相关性。它应该是简单的,但我得到一个错误" mat [i,j]< - result ["估计"]中的错误:   矩阵"上的下标数量不正确 这是我的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按相同的顺序排列)并输出带有p值,估计和测试的excel表,以及一个矩阵只有那些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语句或者如果我只是打印出结果[&#34;估计&#34;],我就不会得到错误。否则,我会一直遇到错误。

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

1 个答案:

答案 0 :(得分:1)

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

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

你不会得到错误