1

I have a [4 x 5] matrix that I've named T2:

T2 <- new("dgTMatrix",
      i = as.integer(c(1,1,0,3,3)),
      j = as.integer(c(2,2,4,0,0)), x=10*1:5, Dim=4:5)

colnames(T2) <- c("Anthony", "Benjamin", "Clara", "Dexter", "Elise")



abc <- data.frame(c("Anthony", "Benjamin"))
colnames(abc) <- c("Names")

I want to subset it to obtain a [4 x 2] matrix:

T2.s1 <- subset.matrix(T2, colnames(T2) %in% abc$Names)

Does anybody know why this does not work?

Many thanks,

Abigail

Community
  • 1
  • 1
Abigail575
  • 175
  • 8
  • 4
    Please take a look at [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), to modify your question, with a smaller sample taken from your data (check `?dput()`). Posting images of your data or no data makes it difficult to impossible for us to help you! – massisenergy Feb 24 '20 at 16:10
  • OK I will try!. – Abigail575 Feb 24 '20 at 16:23
  • 2
    `expr_matrix` is not of class `matrix` but class `dgTMatrix` which does not have a `subset` function definition (AFAIK). Why not use `expr_matrix[, colnames(expr_matrix) %in% barcode_file.s1$barcode]`? – emilliman5 Feb 24 '20 at 16:42
  • 1
    I've tried to make a reproducible example, but I don't know how great it is! Thank you @emilliman5. Your script now has given me a [33694 x 0] matrix so I'm almost there :-) – Abigail575 Feb 24 '20 at 17:01

1 Answers1

1

It is not a data.frame, so you cannot use subset, and there are no subset methods for sparseMatrix.

Just subset it like you will do for a matrix:

 T2[,colnames(T2) %in% abc$Names]
4 x 2 sparse Matrix of class "dgTMatrix"
     Anthony Benjamin
[1,]       .        .
[2,]       .        .
[3,]       .        .
[4,]      90        .
StupidWolf
  • 45,075
  • 17
  • 40
  • 72