I'm trying to subset my seurat object based on colnames. I have gone ahead and labeled each cluster and now I want to subset all the colnames that are in Cancer_human for human_colnames and all the barcodes that are not in Cancer_human for mouse_colnames but get an error.

human_colnames = colnames(scData[,scData$cell_ann == "Cancer_human"])
mouse_colnames = colnames(scData[,!scData$cell_ann == "Cancer_human"])

Error in `[.Seurat`(scData, , scData$cell_ann == "Cancer_human") : Incorrect number of logical values provided to subset cells

I'm filtering on colnames not rownames. I want all the barcodes from the Cancer_human.

  • $\begingroup$ The code could only make sense if the data is a square, equal number of rows and columns. At the moment you are getting index from row comparison, then using that index to subset columns. Also, please provide a reproducible example data for testing, dput(myData). $\endgroup$
    – zx8754
    Aug 17, 2021 at 14:44

2 Answers 2


Answer from @ram-rs, converted from comment:

I don't think it works that way, mmpp. The [ operation needs either a list of names, indexes or a boolean vector for each of the row and column spots to subset along that dimension, and you cannot use a boolean vector based on rows to subset columns (that's what you're doing here). Google how to subset columns in R.


Did you tried the subset function?

# assuming that scData@meta.data looks like this
#         orig.ident  ...  cell_ann
# ATTCAA  sample1          Cancer_human
# ATTTAT  sample1          Cancer_human
# ...
# AAATCA  sampleX          Cancer_mouse
# ATTTAA  sampleX          Cancer_mouse

Idents(scData) <- "cell_ann"
human_subset <- subset(scData, idents = "Cancer_human")
mouse_subset <- subset(scData, idents = "Cancer_mouse") 
mouse_subset <- subset(scData, idents = "Cancer_human", invert = TRUE) # alternative way 

P.S. It is not clear what you are trying to do. You should add to your post how your data looks like.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.