<p>You should be able to use something like this:</p>
<pre><code>Reduce(
    f = function(x, y) {merge(x, y, merge.data = FALSE)},
    x = datasets # list of Seurat objects
)
</code></pre>
<p>This will create a new Seurat object based on the multiple seurat objects in your list. It might be good idea to store the &quot;sample&quot; information within the metadata slots of individual objects. Probably a better approach is to have cell barcodes of each sample prefixed with the sample id.</p>

CC BY-SA 4.0
2023-04-24T09:01:57.350Z
2023-04-24T09:01:57.350Z
haci
<p>I can't reproduce things quickly without a <a href="https://stackoverflow.com/help/minimal-reproducible-example">minimum reproducible example (e.g. with sample data from the package)</a>. However, the error is telling you that R cannot convert the object to a dataframe when trying to use <code>merge()</code> (at least I suspect that this error occurred when you ran <code>merge()</code> since you posted it right under it). I suggest you convert <em>everything you need</em> to a dataframe (there are quite some questions, e.g., on biostars regarding this conversion). <em>Everything you need</em> because a SeuratObject cannot simply be cast to a dataframe, only parts of it, e.g., <code>data_transcripts_seurat@assays$SCT@counts %&gt;% as.matrix() %&gt;% t() %&gt;% as.data.frame()</code> (<a href="https://www.biostars.org/p/482473/" rel="nofollow noreferrer">from this biostars question</a>). However, you might need some other information. Therefore, I suggest you explore the SeuratObject using RStudio and check what information you want to draw from it. After the (interesting) parts of the SeuratObject have been cast to dataframes, e.g., in the for-loop, you can go ahead with merging.</p>

CC BY-SA 4.0
2023-04-24T18:23:12.133Z
There is no need to convert the Seurat object into a data frame, because a `merge()` function exists for Seurat objects.
Cloudberry
CC BY-SA 4.0
2023-04-24T06:36:04.613Z
2023-04-24T06:36:04.613Z
freyberg
<p>The first parameter of <code>merge</code> should be a Seurat object, the second (<code>y</code>) can be one Seurat object or a list of several. Try:</p>
<pre><code>merge(x = datasets[[1]], y = datasets[-1])
</code></pre>
<p>See the <a href="https://satijalab.org/seurat/articles/merge_vignette.html" rel="nofollow noreferrer">merge vignette</a> for more details. You may want to use the <code>add.cell.ids</code> option to be able to tell which dataset each cell originated from.</p>

CC BY-SA 4.0
2023-04-24T18:18:34.043Z
2023-04-24T18:18:34.043Z
Cloudberry
<p>I have a R script that subsets out the tumor cells from 6 different individual seurat objects.</p>
<p>It gets a list of file paths for all RDS files containing the expression data for multiple datasets in a directory.
It creates two empty lists, datasets and dataset_index, to hold the subsets of tumor cells from each dataset and their corresponding indices, respectively.
It loops through each file path, reads the RDS file, and subsets the tumor cells from the expression data.
If the dataset does not contain any tumor cells, it prints a message indicating that fact.
The subsets of tumor cells are appended to the datasets list and their corresponding indices are appended to the dataset_index list.</p>
<pre><code>scfiles = list.files('/LungDataSets/',pattern = '.*SCTransform_Symphony.*.RDS',recursive=TRUE)

scfiles = paste('/LungDataSets/',scfiles,sep='')

datasets = list()
dataset_index = list()

for (i in 1:length(scfiles)) {
    scData = readRDS(scfiles[i])
    
    if (is.element('Tumor', unique(scData$cell_ann)))  {
        tumorData = subset(scData,subset = cell_ann=='Tumor')
        datasets = append(datasets,tumorData)
        dataset_index = append(dataset_index,toString(i))
    }
    else {
        print(c(scfiles[i], 'does not contain Tumor cells'))
    }
        
    
    rm(scData)
    
    
}
</code></pre>
<p>Now I want to merge all those tumor cells from the 6 datasets into one seurat object.</p>
<p>I've tried <code>merge</code> and <code>rbind</code> but they don't work.</p>
<pre><code>megaObject &lt;- merge(datasets)
Error in as.data.frame.default(x[[i]], optional = TRUE) : 
  cannot coerce class ‘structure(&quot;Seurat&quot;, package = &quot;SeuratObject&quot;)’ to a data.frame
</code></pre>

<r><scrnaseq><seurat><merge>
Merging multiple seurat datalist together
# Merging multiple seurat datalist together

(Asked by: username_0 on 2023-04-24)

I have a R script that subsets out the tumor cells from 6 different individual seurat objects.
It gets a list of file paths for all RDS files containing the expression data for multiple datasets in a directory.
It creates two empty lists, datasets and dataset_index, to hold the subsets of tumor cells from each dataset and their corresponding indices, respectively.
It loops through each file path, reads the RDS file, and subsets the tumor cells from the expression data.
If the dataset does not contain any tumor cells, it prints a message indicating that fact.
The subsets of tumor cells are appended to the datasets list and their corresponding indices are appended to the dataset_index list.
scfiles = list.files('/LungDataSets/',pattern = '.*SCTransform_Symphony.*.RDS',recursive=TRUE)

scfiles = paste('/LungDataSets/',scfiles,sep='')

datasets = list()
dataset_index = list()

for (i in 1:length(scfiles)) {
    scData = readRDS(scfiles[i])
    
    if (is.element('Tumor', unique(scData$cell_ann)))  {
        tumorData = subset(scData,subset = cell_ann=='Tumor')
        datasets = append(datasets,tumorData)
        dataset_index = append(dataset_index,toString(i))
    }
    else {
        print(c(scfiles[i], 'does not contain Tumor cells'))
    }
        
    
    rm(scData)
    
    
}

Now I want to merge all those tumor cells from the 6 datasets into one seurat object.
I've tried merge and rbind but they don't work.
megaObject <- merge(datasets)
Error in as.data.frame.default(x[[i]], optional = TRUE) : 
  cannot coerce class ‘structure("Seurat", package = "SeuratObject")’ to a data.frame

**Tags:** r, scrnaseq, seurat, merge

---

## 3 answers

### Answer by username_3 on 2023-04-24. Score: 3 | Accepted answer

The first parameter of merge should be a Seurat object, the second (y) can be one Seurat object or a list of several. Try:
merge(x = datasets[[1]], y = datasets[-1])

See the merge vignette for more details. You may want to use the add.cell.ids option to be able to tell which dataset each cell originated from.


---

### Answer by username_1 on 2023-04-24. Score: 2

You should be able to use something like this:
Reduce(
    f = function(x, y) {merge(x, y, merge.data = FALSE)},
    x = datasets # list of Seurat objects
)

This will create a new Seurat object based on the multiple seurat objects in your list. It might be good idea to store the "sample" information within the metadata slots of individual objects. Probably a better approach is to have cell barcodes of each sample prefixed with the sample id.


---

### Answer by username_2 on 2023-04-24. Score: 0

I can't reproduce things quickly without a [minimum reproducible example (e.g. with sample data from the package)](https://stackoverflow.com/help/minimal-reproducible-example). However, the error is telling you that R cannot convert the object to a dataframe when trying to use `merge()` (at least I suspect that this error occurred when you ran `merge()` since you posted it right under it). I suggest you convert *everything you need* to a dataframe (there are quite some questions, e.g., on biostars regarding this conversion). *Everything you need* because a SeuratObject cannot simply be cast to a dataframe, only parts of it, e.g., `data_transcripts_seurat@assays$SCT@counts %>% as.matrix() %>% t() %>% as.data.frame()` ([from this biostars question](https://www.biostars.org/p/482473/)). However, you might need some other information. Therefore, I suggest you explore the SeuratObject using RStudio and check what information you want to draw from it. After the (interesting) parts of the SeuratObject have been cast to dataframes, e.g., in the for-loop, you can go ahead with merging.

> **Comments:**
> **username_3**: There is no need to convert the Seurat object into a data frame, because a `merge()` function exists for Seurat objects.  

---

