简体   繁体   English

通过向量R中的维名称子集矩阵

[英]Subset a matrix by dimension names in a vector R

I have a vector nodenames as 我有一个矢量节点名称

nodenames <- c("A","B","C","T","N","Z")

I have a square sparse matrix with dimnames as 我有一个带有dimnames的方形稀疏矩阵

Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
  ..@ i       : int [1:4149962] 1 2 3 4 5 9 11 12 13 14 ...
  ..@ p       : int [1:3417] 0 1702 2710 3935 5411 6719 8141 9822 9822 11515 ...
  ..@ Dim     : int [1:2] 3416 3416
  ..@ Dimnames:List of 2
  .. ..$ : chr [1:3416] "A" "B" "AAL" "T" ...
  .. ..$ : chr [1:3416] "A" "B" "AAL" "T" ...
  ..@ x       : num [1:4149962] 2 1 1 3 1 1 2 19 3 2 ...
  ..@ factors : list()

How can I produce a subset of this matrix with dimnames in nodenames? 如何在节点名中使用dimnames生成此矩阵的子集?

You can subset matrices based by index numbers, by dimension names (via a character vector, such as your nodenames ), by logical vectors, and possibly other things that are beyond me. 您可以根据索引号,维度名称(通过字符向量,例如您的nodenames ),逻辑向量以及可能超出我的其他内容来对矩阵进行子集化。

mat1[nodenames, nodenames]
  A  B  C  T  N  Z
A 12 22 42 62 72 82
B 13 23 43 63 73 83
C 15 25 45 65 75 85
T 17 27 47 67 77 87
N 18 28 48 68 78 88
Z 19 29 49 69 79 89

or: 要么:

mat1[which(rownames(mat1)%in% nodenames), which(colnames(mat1) %in% nodenames)]
mat1[rownames(mat1)%in% nodenames, colnames(mat1) %in% nodenames]

I think Tim Riffe's answer is the most direct. 我认为Tim Riffe的回答是最直接的。 If the user were unsure whether the 'nodenames' vector were a subset of both the rownames() and the colnames() values, then this might be a bit safer: 如果用户不确定'nodenames'向量是否是rownames()和colnames()值的子集,那么这可能会更安全一些:

nodenames <- c("A","ZZ","C","T","N","Z")

seq1 <- seq(1:100)
mat1 <- matrix(seq1, 10)
rownames(mat1)<-c("G","A","B","F","C","D","T","N","Z","J")
colnames(mat1)<-c("G","A","B","F","C","D","T","N","Z","J")
mat1[rownames(mat1) %in% nodenames, colnames(mat1) %in% nodenames]
#----------
   A  C  T  N  Z
A 12 42 62 72 82
C 15 45 65 75 85
T 17 47 67 77 87
N 18 48 68 78 88
Z 19 49 69 79 89

For the amended question for objects of class-dgCMatrix I am getting sensible results using the same methods: 对于类-dgCMatrix对象的修正问题,我使用相同的方法获得了明智的结果:

(m <- Matrix(c(0,0,2:0), 3,5))
3 x 5 sparse Matrix of class "dgCMatrix"

[1,] . 1 . . 2
[2,] . . 2 . 1
[3,] 2 . 1 . .

 m@Dimnames <- list(X=letters[1:3], Y=LETTERS[1:5])
 m["a", "B"]
# [1] 1
 m["a", c("A","B")]
# A B 
# 0 1 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM