This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Need help with simple FOR loop

This simple for loop I want it to run the function FindMarkers, which will take as an argument a data identifier (1,2,3 etc..) that it will use to pull data from. I then want it to store the result of the function in immunes.i, where I want I to be the same integer (1,2,3)

So I want an output of 15 files names immunes.0, immunes.1, immunes.2 etc...

Right now Im getting an error though; here is the code:

for (i in 0:14){
immunes.i <- FindMarkers(AllCells.combined, ident.1 = "VEH", ident.2 = "IMQ", verbose = TRUE, group.by="stim", subset.ident = "i")
}

The error i get is "Error in WhichCells.Seurat(object = x, cells = cells, idents = idents, : Cannot find the following identities in the object: i"

If I run the last line by itself it works perfectly, namely:

immunes.0 <- FindMarkers(AllCells.combined, ident.1 = "VEH", ident.2 = "IMQ", verbose = TRUE, group.by="stim", subset.ident = "0")

any advice? I want to use i as a variable within the loop but I don't know the right syntax or way to do it....

seurat rna-seq

2 answers

I would try two things here.

create a list and push the values

immune =list()
 for (i in 0:14){
       immune[i] =FindMarkers(AllCells.combined, ident.1 = "VEH", ident.2 = "IMQ", verbose = TRUE, group.by="stim", subset.ident = i) )
}

or

create multiple object using loop

for (i in 0:14){
  assign(paste0("immune", i), FindMarkers(AllCells.combined, ident.1 = "VEH", ident.2 = "IMQ", verbose = TRUE, group.by="stim", subset.ident = i) ))
}

don't you want

immunes.i <- FindMarkers(AllCells.combined, ident.1 = "VEH", ident.2 = "IMQ", verbose = TRUE, group.by="stim", subset.ident = i)

Accually thank you, this runs the code but it doesn't give me the desired output. It will run all the functions but the output is only 1 file called immunes.i that contains the data from the last run of the loop.

I want the output to be immunes.1, immunes.2, etc...

Log in to answer this question.