Need help with simple FOR loop
2
0
Entering edit mode
4.6 years ago
cook.675 ▴ 220

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 • 3.6k views
ADD COMMENT
4
Entering edit mode
4.6 years ago
Prakash ★ 2.2k

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) ))
}
ADD COMMENT
0
Entering edit mode
4.6 years ago

don't you want

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

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...

ADD REPLY

Login before adding your answer.

Traffic: 1503 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