Hello,
I am practicing R codes in the Seurat tutorials. When I type
for (i in 0:12) {
g[i].marker <- FindConservedMarkers(immune.combined, ident.1 = i,
grouping.var = "stim", verbose = FALSE)
}
It gives me an error as below.
Error: unexpected symbol in: "for (i in 0:12) { g[i].marker"
} Error: unexpected '}' in " }"
How can I fix this error? Please help!
what if you add
g_marker <- 0
before the for loop to initialize the array?https://www.datacamp.com/community/tutorials/tutorial-on-loops-in-r
could you try this command to see what is the output? I assume it's of type string.
Also you can try with a simpler code to see if your loop and your array work as expected, for example these scripts works for me
Numeric array:
String array:
Hi Fatima,
Thank you for your kind advice. I tried the simpler code and found that it works as expected. However, when I type add g_marker <- 0 before the for loop,
it gave me errors as below.
Could you just run
And see what the output is? It might be a complicated output and we need to see the format before deciding on how to save it in an array.
based on the link below the output is a data.frame. https://www.rdocumentation.org/packages/Seurat/versions/3.1.2/topics/FindConservedMarkers
For a simple string output (e.g Gene1) you could add
g_marker <- ""
before the loop instead of 0, if you post a sample output I might be able to help on how to save it in a suitable form. For example if the output is a big data.frame then you can write the result of each iteration in a new csv file or you can use a script like this:Simpler script:
Another simpler but irrelevant script:
https://thomasleeper.com/Rcourse/Tutorials/savingdata.html
https://stackoverflow.com/questions/16566799/change-variable-name-in-for-loop-using-r
Hi Fatima,
Thank you so much for your comments. I add g_marker <- "" before the loop and also got an error as below, which is similar to the result of g_marker <- 0.
Since the output is a data.frame you can do the following as mentioned in my previous comment.
You can read more about paste and assign here: https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/paste https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/assign
You can also use this script to save each result in a file:
Then you can find the following files in your current directory:
Another simple approach can be just manually assigning values 1 to 12 to ident.1:
Hi Fatima,
Thank you for your advice. Your code works very well.
do the following: