我的文件是这样的1367 * 1367长矩阵-
MU101188 MU101310 MU101326 MU10251
MU101188 1 0 0 0
MU101310 0 1 0 0
MU101326 0 0 1 0
MU10251 0 0 0 1
我需要提取其值等于1的所有对。 我正在使用以下R脚本,该脚本为我提供了行号和列号,但我也想要名称
Pmatrix = read.csv ("file.csv", header = TRUE, row.names = 1)
sig_values <- which(Pmatrix==1, arr.in= TRUE)
答案 0 :(得分:0)
基于相同的步骤,我们还可以使用以下逻辑。 (不确定内置函数是否返回行,col为一次性答案)。 还测试了此代码,并在行中存在多个1,并且可以正常工作。
下面的代码段:
Pmatrix = read.csv ("file.csv", header = TRUE, row.names = 1)
sig_values <- which(Pmatrix==1, arr.in= TRUE)
# just check the values
Pmatrix
sig_values
# incase there are multiple 1's in Pmatrix row
# or one need to sort the order for the row-wise display
sig_values<-sig_values[order(sig_values[,1]),]
# remove the above line, incase there are no multiple 1's in input file or no sorting is desired
# code to get the desired rowname and colname
i<-1
while (i <= nrow(sig_values)){
# you can use whatever format and store in variabe or do your processing here
# e.g. my format was (row,col), hence the paste format
row_col<-paste("(",dimnames(Pmatrix)[[1]][sig_values[i,1]],",",dimnames(Pmatrix)[[2]][sig_values[i,2]],")")
print(row_col)
i<-i+1
}
#Output
[1] "( MU101188 , MU101188 )"
[1] "( MU101310 , MU101310 )"
[1] "( MU101326 , MU101326 )"
[1] "( MU10251 , MU10251 )"