Answers.Body: <p>If you have single-dimension per-cell metadata, and it's arranged identically to the cell order in the Seurat object, I find it easier to use the double bracket notation to add metadata to a Seurat object. For example:</p>
  <pre><code>metadata<span class="math-container">$barcodes -&gt; pbmc[["barcodes"]]
  metadata$</span>libcodes -&gt; pbmc[[&quot;libcodes&quot;]]
  metadata$samples -&gt; pbmc[[&quot;samples&quot;]]
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-07-09T02:44:28.763Z
Answers.LastActivityDate: 2021-07-09T02:44:28.763Z
Answers.UserDisplayName: gringer
Answers.Body: <p><em>Answer from @user438383, converted from comment:</em></p>
  <p>Did you try <a href="https://www.rdocumentation.org/packages/Seurat/versions/3.1.4/topics/AddMetaData" rel="nofollow noreferrer">the AddMetaData function</a>? <a href="https://stackoverflow.com/questions/42279766/add-metadata-to-seurat-object">This post</a> may also be useful.</p>
  <pre><code>cluster_letters &lt;- LETTERS[Idents(object = pbmc_small)]
  names(cluster_letters) &lt;- colnames(x = pbmc_small)
  pbmc_small &lt;- AddMetaData(
    object = pbmc_small,
    metadata = cluster_letters,
    col.name = 'letter.idents'
  )
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-07-08T21:35:48.810Z
Answers.LastActivityDate: 2023-09-16T22:08:18.357Z
Answers.LastEditDate: 2023-09-16T22:08:18.357Z
Answers.UserDisplayName: gringer
Body: <p>I have a Seurat object of 8 patients. I want to add metadata to that so that I have origin of each cell. At the moment UMAP just shows a bunch of cells while I want to color clusters by sample</p>
  <p>This is my metadata</p>
  <pre><code>&gt; str(head(metadata))
  'data.frame':   6 obs. of  3 variables:
   <span class="math-container">$ barcodes: chr  "AAACAGCCAAACTGCC-5" "AAACAGCCAACAGGAT-1" "AAACAGCCAAGATTCT-1" "AAACAGCCAAGGTATA-1" ...
   $</span> libcodes: Factor w/ 8 levels &quot;1&quot;,&quot;2&quot;,&quot;3&quot;,&quot;4&quot;,..: 5 1 1 1 5 7
   $ samples : chr  &quot;PN0252_0005&quot; &quot;PN0252_0001&quot; &quot;PN0252_0001&quot; &quot;PN0252_0001&quot; ...
  &gt; 
  
  &gt; pbmc
  An object of class Seurat 
  36601 features across 26473 samples within 1 assay 
  Active assay: RNA (36601 features, 0 variable features)
  &gt; 
  
  &gt; dim(metadata)
  [1] 26473     3
  &gt; 
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-09-17T19:59:31.503Z
Comments.Text: Thanks @angel much appreciated
Comments.UserDisplayName: M__
Tags: <scrnaseq><seurat><10x-genomics>
Title: Adding metadata to Seurat object
ThreadText: # Adding metadata to Seurat object
  
  (Asked by: username_0 on 2021-07-07)
  
  I have a Seurat object of 8 patients. I want to add metadata to that so that I have origin of each cell. At the moment UMAP just shows a bunch of cells while I want to color clusters by sample
  
  This is my metadata
  
  ```
  > str(head(metadata))
  'data.frame':   6 obs. of  3 variables:
   $ barcodes: chr  "AAACAGCCAAACTGCC-5" "AAACAGCCAACAGGAT-1" "AAACAGCCAAGATTCT-1" "AAACAGCCAAGGTATA-1" ...
   $ libcodes: Factor w/ 8 levels "1","2","3","4",..: 5 1 1 1 5 7
   $ samples : chr  "PN0252_0005" "PN0252_0001" "PN0252_0001" "PN0252_0001" ...
  > 
  
  > pbmc
  An object of class Seurat 
  36601 features across 26473 samples within 1 assay 
  Active assay: RNA (36601 features, 0 variable features)
  > 
  
  > dim(metadata)
  [1] 26473     3
  > 
  
  ```
  
  **Tags:** scrnaseq, seurat, 10x-genomics
  
  > **Comments:**
  > **username_1**: Thanks @angel much appreciated  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2021-07-09. Score: 2 | Accepted answer
  
  If you have single-dimension per-cell metadata, and it's arranged identically to the cell order in the Seurat object, I find it easier to use the double bracket notation to add metadata to a Seurat object. For example:
  
  ```
  metadata$barcodes -> pbmc[["barcodes"]]
  metadata$libcodes -> pbmc[["libcodes"]]
  metadata$samples -> pbmc[["samples"]]
  
  ```
  
  
  ---
  
  ### Answer by username_2 on 2021-07-08. Score: 2
  
  *Answer from @user438383, converted from comment:*
  
  Did you try [the AddMetaData function](https://www.rdocumentation.org/packages/Seurat/versions/3.1.4/topics/AddMetaData)? [This post](https://stackoverflow.com/questions/42279766/add-metadata-to-seurat-object) may also be useful.
  
  ```
  cluster_letters <- LETTERS[Idents(object = pbmc_small)]
  names(cluster_letters) <- colnames(x = pbmc_small)
  pbmc_small <- AddMetaData(
    object = pbmc_small,
    metadata = cluster_letters,
    col.name = 'letter.idents'
  )
  
  ```
  
  
  ---
  
  