<p>The clusters information, together with additional information on each cells (%mito, sample name, group, orig.ident,...) are stored inside <code>seuratObject@meta.data</code>.
<code>write.csv(seuratObject@meta.data,&quot;./seurat_metadata.csv&quot;)</code> will dump everything to a file.</p>
<p>The method @osmoc suggested, is using built in seurat functions to interact with <code>seuratObject@meta.data</code>.
<code>seuratObject$graph_name_resolution</code> points to <code>seuratObject@meta.data$graph_name_resolution</code>.</p>
<p>I personally prefer to use the metadata table, as I have more control on how I interact with the data.</p>

CC BY-SA 4.0
2021-10-28T06:09:55.957Z
Thank you very much !
Apppii092
CC BY-SA 4.0
2021-10-28T14:19:27.763Z
Using the `meta.data` slot is the best answer. The behavior of various shortcut methods can change between different versions of Seurat and even based on how you interact with your Seurat object. More generally, it is a bad idea to take columns from different sources when constructing a data frame.
burger
CC BY-SA 4.0
2021-10-27T09:21:28.803Z
2021-10-27T09:21:28.803Z
fra
<p>I've recently done this with my own Seurat data; here's the process:</p>
<pre><code># load tidyverse packages
library(tidyverse)
# create cluster ID table
tibble(cellID = colnames(pbmc), clusterID = Idents(pbmc)) %&gt;%
  # write out to a file, with today's date
  write_csv(file = sprintf(&quot;cluster_mappings_%s.csv&quot;, Sys.Date()))
</code></pre>

CC BY-SA 4.0
2021-10-28T06:08:44.100Z
Thank you very much !!
Apppii092
CC BY-SA 4.0
2021-10-26T22:04:25.873Z
2021-10-26T22:04:25.873Z
gringer
<p>The clusters are saved in the field <code>object$seurat_clusters</code> where <code>object</code> represents your Seurat object.</p>
<p>You can also get them running the command <code>Idents(object)</code>.</p>
<p><strong>NOTE</strong>: if you run the command <code>FindClusters</code> on different SNN graphs, or different resolutions, you need to specify the clusters by <code>object$graph_name_resolution</code>.
Every time you run <code>FindClusters</code>, the idents of the cells are updated to the latest results, same for <code>seurat_clusters</code>.</p>
<p>For instance, let's say that you have run the command <code>FindNeighbors</code> on the <code>RNA</code> assay (default value).<br />
Then you run <code>FindClusters</code> with <code>graph = &quot;RNA_snn&quot;</code> and <code>resolution=1</code>.<br />
In that case, the clusters are in <code>object$RNA_snn_1</code>.</p>
<p>Once you have the clusters, you can save them as a CSV:</p>
<pre><code>clusters &lt;-  &lt;here you use one of the described methods&gt;
# data.frame
clust.df &lt;- data.frame(cellID = colnames(object), clusters = clusters)
write.csv(clust.df, &lt;path.of.the.csv.file&gt;)
</code></pre>

CC BY-SA 4.0
2021-10-28T02:53:26.550Z
Thank you very much !
Apppii092
CC BY-SA 4.0
2021-10-27T09:10:46.427Z
2021-10-27T09:16:21.410Z
2021-10-27T09:16:21.410Z
piplustwo
<p>I am doing scRNAseq analysis with Seurat.</p>
<p>I clustered the cells using the FindClusters() function.</p>
<p>What I want to do is to export information about which cells belong to which clusters to a CSV file.</p>
<p>In a Seurat object, we can show the cluster IDs by using Idents(・), but I have no idea how to export this to CSV files.</p>
<p>I would be grateful if you could show this by using the PMBC data (<a href="https://satijalab.org/seurat/articles/pbmc3k_tutorial.html" rel="nofollow noreferrer">https://satijalab.org/seurat/articles/pbmc3k_tutorial.html</a>) as an example.</p>

<seurat><single-cell><clustering>
In Seurat, how can I export cluster IDs to csv files
# In Seurat, how can I export cluster IDs to csv files

(Asked by: username_0 on 2021-10-24)

I am doing scRNAseq analysis with Seurat.

I clustered the cells using the FindClusters() function.

What I want to do is to export information about which cells belong to which clusters to a CSV file.

In a Seurat object, we can show the cluster IDs by using Idents(・), but I have no idea how to export this to CSV files.

I would be grateful if you could show this by using the PMBC data (<https://satijalab.org/seurat/articles/pbmc3k_tutorial.html>) as an example.

**Tags:** seurat, single-cell, clustering

---

## 3 answers

### Answer by username_1 on 2021-10-27. Score: 2 | Accepted answer

The clusters information, together with additional information on each cells (%mito, sample name, group, orig.ident,...) are stored inside `seuratObject@meta.data`.
`write.csv(seuratObject@meta.data,"./seurat_metadata.csv")` will dump everything to a file.

The method @osmoc suggested, is using built in seurat functions to interact with `seuratObject@meta.data`.
`seuratObject$graph_name_resolution` points to `seuratObject@meta.data$graph_name_resolution`.

I personally prefer to use the metadata table, as I have more control on how I interact with the data.

> **Comments:**
> **username_0**: Thank you very much !  
> **username_2**: Using the `meta.data` slot is the best answer. The behavior of various shortcut methods can change between different versions of Seurat and even based on how you interact with your Seurat object. More generally, it is a bad idea to take columns from different sources when constructing a data frame.  

---

### Answer by username_3 on 2021-10-26. Score: 2

I've recently done this with my own Seurat data; here's the process:
# load tidyverse packages
library(tidyverse)
# create cluster ID table
tibble(cellID = colnames(pbmc), clusterID = Idents(pbmc)) %>%
  # write out to a file, with today's date
  write_csv(file = sprintf("cluster_mappings_%s.csv", Sys.Date()))

> **Comments:**
> **username_0**: Thank you very much !!  

---

### Answer by username_4 on 2021-10-27. Score: 0

The clusters are saved in the field object$seurat_clusters where object represents your Seurat object.
You can also get them running the command Idents(object).
NOTE: if you run the command FindClusters on different SNN graphs, or different resolutions, you need to specify the clusters by object$graph_name_resolution.
Every time you run FindClusters, the idents of the cells are updated to the latest results, same for seurat_clusters.
For instance, let's say that you have run the command FindNeighbors on the RNA assay (default value).
Then you run FindClusters with graph = "RNA_snn" and resolution=1.
In that case, the clusters are in object$RNA_snn_1.
Once you have the clusters, you can save them as a CSV:
clusters <-  
# data.frame
clust.df <- data.frame(cellID = colnames(object), clusters = clusters)
write.csv(clust.df, )

> **Comments:**
> **username_0**: Thank you very much !  

---

