Extracting column from one seurat object based on common column and adding to the other object
0
0
Entering edit mode
3 months ago
Varun Gupta ★ 1.3k

I Have 2 seurat objects obj1 and obj2. Both the objects have Barcode column in it, but obj2 has condition_cluster column as well. I want to add condition_cluster column in the metadata of obj1 based on Barcode column. I am using merge function this way:

 obj1@metadata <-   merge(obj1@meta.data, obj2@meta.data[,c("Barcode","condition_cluster")], by = "Barcode", all.x = TRUE)

I loose the rownames info as well as my Dimplot gives error with out of range values which should not be the case. What am I doing wrong?

R SEURAT • 357 views
ADD COMMENT
1
Entering edit mode

When you use the merge function to combine metadata from two Seurat objects, it can lead to loss of row names and may create mismatches in your data if not handled properly. The merge function typically returns a data frame without retaining the row names from the original Seurat objects. Instead of directly assigning the result of the merge function to obj1@metadata, you should merge the metadata and then assign it back to obj1@meta.data, while ensuring to keep the row names. You can do the following:

rownames(obj1@meta.data) <- obj1@meta.data$Barcode

merged_meta <- merge(obj1@meta.data, obj2@meta.data[, c("Barcode", "condition_cluster")], by = "row.names", all.x = TRUE)

rownames(merged_meta) <- merged_meta$Row.names
merged_meta$Row.names <- NULL

obj1@meta.data <- merged_meta
ADD REPLY

Login before adding your answer.

Traffic: 1799 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6