<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>

CC BY-SA 4.0
2022-01-07T14:48:36.670Z
2022-01-07T15:15:22.237Z
2022-01-07T15:15:22.237Z
fra
<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>

CC BY-SA 4.0
2024-01-29T18:10:38.973Z
2024-01-29T18:10:38.973Z
gringer
<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>

CC BY-SA 4.0
2021-08-17T14:44:59.950Z
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)`.
zx8754
CC BY-SA 4.0
2024-01-28T10:05:32.257Z
@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`.
jan-glx
<r><seurat><single-cell>
Subsetting a Seurat object based on colnames
# 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.
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**: @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`.  

---

## 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"]


---

