<p>For loops in most languages can be done in a variety of ways. What you have currently coded, loops over values 1 to 3 by using</p>

<pre class="lang-r prettyprint-override"><code>for (i in 1:length(fdr.list)){
...
}
</code></pre>

<p>However, you probably want to use a different form and loop over each value in that vector using something more like:</p>

<pre class="lang-r prettyprint-override"><code>for (fdr in fdr.nums.vector){
...
}
</code></pre>

<p>This way you have convenient access to the FDR value in each loop via the <code>fdr</code> variable. Which you can then use in constructing your output filenames. Thus your code would become something like:</p>

<pre class="lang-r prettyprint-override"><code>fdr.nums.vector = c(5,10,20)

for (fdr in fdr.nums.vector){
  atac.glmtop.males.opening.fdr[fdr] &lt;- topTags(
    atac.glmfit.males,
    n = nrow(atac.dge$counts),
    sort.by = "none",
    adjust.method = "fdr"
  )$table %&gt;%
  tibble::rownames_to_column("peakco") %&gt;%
  filter(logFC &lt; 0) %&gt;%
  filter(FDR &lt; (fdr/100)) %&gt;% 
  merge(.,annotated.atac.pbmc.narrow, by="peakco" ) %&gt;%
  tibble::column_to_rownames("peakco")

  atac.glmtop.males.opening.fdr[fdr]

  write.table(
    atac.glmtop.males.fdr[fdr].opening[,c("chr", "start","end")], 
    file = paste0("~/Downloads/sexM.age.groupHO.fdr",fdr, ".opening.bed"), 
    sep = "\t",
    quote = FALSE,
    row.names = FALSE,
    col.names = FALSE
  )
  write.table(
    atac.glmtop.males.fdr[fdr].opening["GeneName"],
    file = "~/Downloads/sexM.age.groupHO.fdr"+ fdr + ".opening.GeneNames.txt",
    sep = "\t",
    quote = FALSE,
    row.names = FALSE,
    col.names = FALSE
  )
}
</code></pre>

<h1>Advancing From <code>for</code> Loops to <code>apply</code></h1>

<p>As a beginner, <code>for</code> loops seem quite logical and easy to understand. However, R has a more powerful way to achieve the same thing through what is called "vectorisation" using the <code>apply</code> function or one of it's relatives <code>lapply</code>, <code>sapply</code> etc. Once you have understood and mastered <code>for</code> loops, you should take a look at <code>apply</code>, especially if you ever need to do a <code>for</code> loop many times or you find yourself doing nested <code>for</code> loops. <code>R</code> is notoriously slow with <code>for</code> loops and <code>apply</code> will likely be many times faster (potentially orders of magnitude). Here are some links for when you are ready to advance from <code>for</code> loops to <code>vectorisation</code>.</p>

<ul>
<li><a href="https://www.r-bloggers.com/how-to-write-the-first-for-loop-in-r/" rel="nofollow noreferrer">https://www.r-bloggers.com/how-to-write-the-first-for-loop-in-r/</a></li>
<li><a href="https://www.r-bloggers.com/using-apply-sapply-lapply-in-r/" rel="nofollow noreferrer">https://www.r-bloggers.com/using-apply-sapply-lapply-in-r/</a></li>
</ul>

CC BY-SA 4.0
2018-06-28T10:16:01.207Z
You are right about vectorisation, however, apply is not vectorisation but uses for loops, see this for details: https://www.r-bloggers.com/vectorisation-is-your-best-friend-replacing-many-elements-in-a-character-vector/
or the document 'The R Inferno'.
Peter
CC BY-SA 4.0
2018-06-28T15:58:40.027Z
“As a beginner, `for` loops seem quite logical and easy to understand” — this is really only true for people who are used to imperative programming. Properly taught, a functional-first approach is simpler for beginners. I strongly recommend skipping `for` loops in teaching and learning (or at least postponing them until *much* later in a course). Especially since, in R, they require all kinds of special explanations.
Konrad Rudolph
CC BY-SA 4.0
2018-06-28T00:25:42.373Z
2018-06-28T00:25:42.373Z
Nathan S. Watson-Haigh
<p>This is probably a really quick/simple fix but I am a Noob to R programming/programming in general. I am trying to create a for loop that runs the test multiple times with different FDR values (5,10,20). How do I make it run 3 times with different values and then produce 3 different datasets with names like... <code>atac.glmtop.males.opening.fdr5</code>, <code>atac.glmtop.males.opening.fdr10</code>, <code>atac.glmtop.males.opening.fdr20</code>? Also is there a way to make the <code>write.table</code> function work so that it exports files with the fdr5, fdr10, fdr20 in the name?</p>

<pre class="lang-r prettyprint-override"><code>fdr.nums = c("5","10","20")
fdr.nums.vector = c(5,10,20)

for (i in 1:length(fdr.list)){
  atac.glmtop.males.opening.fdr[i] &lt;- topTags(
    atac.glmfit.males,
    n = nrow(atac.dge$counts),
    sort.by = "none",
    adjust.method = "fdr"
  )$table %&gt;%
  tibble::rownames_to_column("peakco") %&gt;%
  filter(logFC &lt; 0) %&gt;%
  filter(FDR &lt; (fdr.vector[i]/100)) %&gt;% 
  merge(.,annotated.atac.pbmc.narrow, by="peakco" ) %&gt;%
  tibble::column_to_rownames("peakco")

  atac.glmtop.males.opening.fdr[i]

  write.table(
    atac.glmtop.males.fdr[i].opening[,c("chr", "start","end")], 
    file = paste0("~/Downloads/sexM.age.groupHO.fdr",i, ".opening.bed"), 
    sep = "\t",
    quote = FALSE,
    row.names = FALSE,
    col.names = FALSE
  )
  write.table(
    atac.glmtop.males.fdr[i].opening["GeneName"],
    file = "~/Downloads/sexM.age.groupHO.fdr"+ i + ".opening.GeneNames.txt",
    sep = "\t",
    quote = FALSE,
    row.names = FALSE,
    col.names = FALSE
  )
}
</code></pre>

CC BY-SA 4.0
2018-06-28T06:54:44.013Z
I'm voting to close this question as off-topic because it is not about bioinformatics.
llrs
<r>
How do I create a for loop to filter through different FDR values?
# How do I create a for loop to filter through different FDR values?

(Asked by: username_0 on 2018-06-27)

This is probably a really quick/simple fix but I am a Noob to R programming/programming in general. I am trying to create a for loop that runs the test multiple times with different FDR values (5,10,20). How do I make it run 3 times with different values and then produce 3 different datasets with names like... atac.glmtop.males.opening.fdr5, atac.glmtop.males.opening.fdr10, atac.glmtop.males.opening.fdr20? Also is there a way to make the write.table function work so that it exports files with the fdr5, fdr10, fdr20 in the name?
fdr.nums = c("5","10","20")
fdr.nums.vector = c(5,10,20)

for (i in 1:length(fdr.list)){
  atac.glmtop.males.opening.fdr[i] <- topTags(
    atac.glmfit.males,
    n = nrow(atac.dge$counts),
    sort.by = "none",
    adjust.method = "fdr"
  )$table %>%
  tibble::rownames_to_column("peakco") %>%
  filter(logFC < 0) %>%
  filter(FDR < (fdr.vector[i]/100)) %>% 
  merge(.,annotated.atac.pbmc.narrow, by="peakco" ) %>%
  tibble::column_to_rownames("peakco")

  atac.glmtop.males.opening.fdr[i]

  write.table(
    atac.glmtop.males.fdr[i].opening[,c("chr", "start","end")], 
    file = paste0("~/Downloads/sexM.age.groupHO.fdr",i, ".opening.bed"), 
    sep = "\t",
    quote = FALSE,
    row.names = FALSE,
    col.names = FALSE
  )
  write.table(
    atac.glmtop.males.fdr[i].opening["GeneName"],
    file = "~/Downloads/sexM.age.groupHO.fdr"+ i + ".opening.GeneNames.txt",
    sep = "\t",
    quote = FALSE,
    row.names = FALSE,
    col.names = FALSE
  )
}

**Tags:** r
> **Comments:**
> **username_1**: I'm voting to close this question as off-topic because it is not about bioinformatics.  

---

## 1 answers

### Answer by username_2 on 2018-06-28. Score: 2

For loops in most languages can be done in a variety of ways. What you have currently coded, loops over values 1 to 3 by using
for (i in 1:length(fdr.list)){
...
}

However, you probably want to use a different form and loop over each value in that vector using something more like:
for (fdr in fdr.nums.vector){
...
}

This way you have convenient access to the FDR value in each loop via the fdr variable. Which you can then use in constructing your output filenames. Thus your code would become something like:
fdr.nums.vector = c(5,10,20)

for (fdr in fdr.nums.vector){
  atac.glmtop.males.opening.fdr[fdr] <- topTags(
    atac.glmfit.males,
    n = nrow(atac.dge$counts),
    sort.by = "none",
    adjust.method = "fdr"
  )$table %>%
  tibble::rownames_to_column("peakco") %>%
  filter(logFC < 0) %>%
  filter(FDR < (fdr/100)) %>% 
  merge(.,annotated.atac.pbmc.narrow, by="peakco" ) %>%
  tibble::column_to_rownames("peakco")

  atac.glmtop.males.opening.fdr[fdr]

  write.table(
    atac.glmtop.males.fdr[fdr].opening[,c("chr", "start","end")], 
    file = paste0("~/Downloads/sexM.age.groupHO.fdr",fdr, ".opening.bed"), 
    sep = "\t",
    quote = FALSE,
    row.names = FALSE,
    col.names = FALSE
  )
  write.table(
    atac.glmtop.males.fdr[fdr].opening["GeneName"],
    file = "~/Downloads/sexM.age.groupHO.fdr"+ fdr + ".opening.GeneNames.txt",
    sep = "\t",
    quote = FALSE,
    row.names = FALSE,
    col.names = FALSE
  )
}

Advancing From for Loops to apply
As a beginner, for loops seem quite logical and easy to understand. However, R has a more powerful way to achieve the same thing through what is called "vectorisation" using the apply function or one of it's relatives lapply, sapply etc. Once you have understood and mastered for loops, you should take a look at apply, especially if you ever need to do a for loop many times or you find yourself doing nested for loops. R is notoriously slow with for loops and apply will likely be many times faster (potentially orders of magnitude). Here are some links for when you are ready to advance from for loops to vectorisation.

https://www.r-bloggers.com/how-to-write-the-first-for-loop-in-r/
https://www.r-bloggers.com/using-apply-sapply-lapply-in-r/

> **Comments:**
> **username_3**: You are right about vectorisation, however, apply is not vectorisation but uses for loops, see this for details: https://www.r-bloggers.com/vectorisation-is-your-best-friend-replacing-many-elements-in-a-character-vector/ or the document 'The R Inferno'.  
> **username_4**: “As a beginner, `for` loops seem quite logical and easy to understand” — this is really only true for people who are used to imperative programming. Properly taught, a functional-first approach is simpler for beginners. I strongly recommend skipping `for` loops in teaching and learning (or at least postponing them until *much* later in a course). Especially since, in R, they require all kinds of special explanations.  

---

