列名的子矩阵
Submatrix by colname
我有一个带有行名和列名的 matrix
,我想将其缩减为 submatrix
某些行/列名。指定的名称位于 ~20
值的字符串向量中,而矩阵具有 55.000 rownames
和 805 colnames
。我怎样才能在 R 中有效地做到这一点?
IFNGenes = c('TIMM10','UBE2L6','MX1','IF16','IFI44L','IFIT3','ISG15','OAS1','RSAD2','IFI44','OAS3','DOX58','HERC5','BATF2','LIPA','RSAD2.1')
subMatrix = theMatrix[,IFNGenes]
Error in theMatrix[, IFNGenes] : subscript out of bounds
如果我理解你的问题,你正在寻找这样的东西:
x <- matrix(1:100, 10)
rownames(x) <- LETTERS[1:10]
colnames(x) <- letters[1:10]
x[c("C", "F", "A"), c("d", "b", "e")]
您还可以这样做:
x[c("C", "F", "A"), c(4, 2, 5)]
如果您使用矩阵中不存在的 rowname-index(同名),则会出现错误:
> x[c("C", "F", "XX"), c(4, 2, 5)]
Error in x[c("C", "F", "XX"), c(4, 2, 5)] : subscript out of bounds
您可以找到如下索引:
r <- c("C", "F", "XX")
r[which(! r %in% rownames(x))]
对于您必须测试的数据:
IFNGenes[which(! IFNGenes %in% colnames(theMatrix))]
我有一个带有行名和列名的 matrix
,我想将其缩减为 submatrix
某些行/列名。指定的名称位于 ~20
值的字符串向量中,而矩阵具有 55.000 rownames
和 805 colnames
。我怎样才能在 R 中有效地做到这一点?
IFNGenes = c('TIMM10','UBE2L6','MX1','IF16','IFI44L','IFIT3','ISG15','OAS1','RSAD2','IFI44','OAS3','DOX58','HERC5','BATF2','LIPA','RSAD2.1')
subMatrix = theMatrix[,IFNGenes]
Error in theMatrix[, IFNGenes] : subscript out of bounds
如果我理解你的问题,你正在寻找这样的东西:
x <- matrix(1:100, 10)
rownames(x) <- LETTERS[1:10]
colnames(x) <- letters[1:10]
x[c("C", "F", "A"), c("d", "b", "e")]
您还可以这样做:
x[c("C", "F", "A"), c(4, 2, 5)]
如果您使用矩阵中不存在的 rowname-index(同名),则会出现错误:
> x[c("C", "F", "XX"), c(4, 2, 5)]
Error in x[c("C", "F", "XX"), c(4, 2, 5)] : subscript out of bounds
您可以找到如下索引:
r <- c("C", "F", "XX")
r[which(! r %in% rownames(x))]
对于您必须测试的数据:
IFNGenes[which(! IFNGenes %in% colnames(theMatrix))]