# How do I create a for loop to filter through different FDR values?

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")],
sep = "\t",
quote = FALSE,
row.names = FALSE,
col.names = FALSE
)
write.table(
atac.glmtop.males.fdr[i].opening["GeneName"],
sep = "\t",
quote = FALSE,
row.names = FALSE,
col.names = FALSE
)
}
• I'm voting to close this question as off-topic because it is not about bioinformatics.
– llrs
Jun 28 '18 at 6:54

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")],
sep = "\t",
quote = FALSE,
row.names = FALSE,
col.names = FALSE
)
write.table(
atac.glmtop.males.fdr[fdr].opening["GeneName"],