I have a matrix
with row and col names and want to reduce it to a submatrix
certain row / colnames. The specified names are in a string vector of ~20
values, while the matrix has 55.000 rownames
and 805 colnames
. How can I do this efficiently in 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
If I understand your question right you are looking for something like this:
x <- matrix(1:100, 10)
rownames(x) <- LETTERS[1:10]
colnames(x) <- letters[1:10]
x[c("C", "F", "A"), c("d", "b", "e")]
You can also do:
x[c("C", "F", "A"), c(4, 2, 5)]
If you use an rowname-index (same for colnames), which not exists in the matrix, you get an error:
> x[c("C", "F", "XX"), c(4, 2, 5)]
Error in x[c("C", "F", "XX"), c(4, 2, 5)] : subscript out of bounds
You can find such indizes as follows:
r <- c("C", "F", "XX")
r[which(! r %in% rownames(x))]
For your data you have to test:
IFNGenes[which(! IFNGenes %in% colnames(theMatrix))]