Entering edit mode
11 months ago
kousi31
▴
100
I want to add patient information to individual Seurat objects before merging them. I have a the patient characteristics as dataframe named covid-19
Adding info to individual obj as below works,
cov01 <- AddMetaData(cov01, metadata = c(covid_19[1,]), col.name =
colnames(covid_19))
However, when I I try to use forloop to automate, I am getting an error.
Error in UseMethod(generic = "AddMetaData", object = object) :
no applicable method for 'AddMetaData' applied to an object of class "list"
S_obj = c(cov01, cov02, cov03, cov04, cov07, cov08, cov09, cov10, cov11, cov12, cov17, cov18) # name of my Seurat objects
for (i in 1:12) {
S_obj[i] <- AddMetaData(S_obj[i], metadata = c(covid_19[i,]), col.name = colnames(covid_19))
}
How to iterate through Seurat objects?
What is the output to
class(S_obj[1])
? Also, try usingas.vector(covid_19[i])
instead ofc(covid_19[i,])
.When I run the loop with two square brackets S_obj[[i]], it runs without taking the object name and without throwing error. When I check the metadata the patient info is not added.
The problem is the forloop doesn't take the object name from the list during iteration
What do you mean by "object name"?
First off, ensure it works for one object before you run a loop. Change your code to this:
How I picked
unlist
:By object name I mean the name I gave to Seurat objects for my individual samples (I have 12 'individuals' in the dataset).
The code works fine for individual samples, e.g.
However, the forloop doesn't iterate through the 12 Seurat objects.
If I run individually like this it works;
But the forloop doesn't.
I combined your comments into a single comment. Please note that "forloop" is not a thing, it's a for loop - 2 words.
Anyway, this is quite odd. Can I ask you try one last thing - use
list()
instead ofc()
to create theS_obj
list.