-1

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
lmo
  • 37,904
  • 9
  • 56
  • 69
El Dude
  • 5,328
  • 11
  • 54
  • 101
  • 3
    How about a small reproducible example? I guess you can use the `row/column` indexing. – akrun Jan 06 '16 at 18:45
  • Code is above, I tried and get an subscript error. Could it be that some of the Genes are not in the colnames? – El Dude Jan 06 '16 at 18:55

1 Answers1

4

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))]
jogo
  • 12,469
  • 11
  • 37
  • 42