Answers.Body: <p>Did you tried the <code>subset</code> function?</p>
  <pre><code># 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) &lt;- &quot;cell_ann&quot;
  human_subset &lt;- subset(scData, idents = &quot;Cancer_human&quot;)
  mouse_subset &lt;- subset(scData, idents = &quot;Cancer_mouse&quot;) 
  mouse_subset &lt;- subset(scData, idents = &quot;Cancer_human&quot;, invert = TRUE) # alternative way 
  </code></pre>
  <p>P.S. It is not clear what you are trying to do. You should add to your post how your data looks like.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-01-07T14:48:36.670Z
Answers.LastActivityDate: 2022-01-07T15:15:22.237Z
Answers.LastEditDate: 2022-01-07T15:15:22.237Z
Answers.UserDisplayName: fra
Answers.Body: <p>If you want the column names to be preserved, you can subset the column names as a vector:</p>
  <pre><code>human_colnames = colnames(scData)[scData<span class="math-container">$cell_ann == "Cancer_human"]
  mouse_colnames = colnames(scData)[!scData$</span>cell_ann == &quot;Cancer_human&quot;]
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-01-29T18:10:38.973Z
Answers.LastActivityDate: 2024-01-29T18:10:38.973Z
Answers.UserDisplayName: gringer
Body: <p>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.</p>
  <pre class="lang-r prettyprint-override"><code>human_colnames = colnames(scData[,scData<span class="math-container">$cell_ann == "Cancer_human"])
  mouse_colnames = colnames(scData[,!scData$</span>cell_ann == &quot;Cancer_human&quot;])
  
  Error in `[.Seurat`(scData, , scData$cell_ann == &quot;Cancer_human&quot;) : Incorrect number of logical values provided to subset cells
  </code></pre>
  <p>I'm filtering on colnames not rownames. I want all the barcodes from the Cancer_human.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-08-17T14:44:59.950Z
Comments.Text: 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)`.
Comments.UserDisplayName: zx8754
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-01-28T10:05:32.257Z
Comments.Text: @zx8754 i agree for the reproducible example (consider the pbmc data set that comes with Seurat) but not the square argument (it would be correct for matrices) but Seurat objects contain multiple things and is implemented such that for a Seurat object `so`, `so[, i]` and so[[j]][i] both give information about cell `i`.
Comments.UserDisplayName: jan-glx
Tags: <r><seurat><single-cell>
Title: Subsetting a Seurat object based on colnames
ThreadText: # Subsetting a Seurat object based on colnames
  
  (Asked by: username_0 on 2021-08-13)
  
  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.
  
  ```r
  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.
  
  **Tags:** r, seurat, single-cell
  
  > **Comments:**
  > **username_1**: 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)`.  
  > **username_2**: @username_1 i agree for the reproducible example (consider the pbmc data set that comes with Seurat) but not the square argument (it would be correct for matrices) but Seurat objects contain multiple things and is implemented such that for a Seurat object `so`, `so[, i]` and so[[j]][i] both give information about cell `i`.  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_3 on 2022-01-07. Score: 0
  
  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.
  
  
  ---
  
  ### Answer by username_4 on 2024-01-29. Score: 0
  
  If you want the column names to be preserved, you can subset the column names as a vector:
  
  ```
  human_colnames = colnames(scData)[scData$cell_ann == "Cancer_human"]
  mouse_colnames = colnames(scData)[!scData$cell_ann == "Cancer_human"]
  
  ```
  
  
  ---
  
  