Hi guys! I need help in setting a `for` loop in R (I'm quite new programming in R). I would like to add the same header to all the files that match a concrete pattern inside a folder. 

To get the list of files I'm using the following code:

    filelist <- list.files(pattern = "DESeq2_result*")

And this is the `for` loop I am trying to implement:

    for (i in seq_along(filelist)) {
       names[[i]] <- a
       out [i]
}

where `a` is a vector that I defined with the names of the different columns: 

    a <- c("gene_id", "baseMean", "log2FC",
           "SD", "WaldStatistic", "pval", "padj")

If you have any tutorial/page to help me to learn and practise my ability to code functions and loops in R would be so much appreciated.

Thank you so much in advance!

Jordi

Use `col.names = ` argument when reading the files, then write out, something like this:


    for(i in list.files(pattern = "DESeq2_result_*"))
      write.table(read.table(i, col.names = c("gene_id", "baseMean", "log2FC",
                                              "SD", "WaldStatistic", "pval", "padj")), i)

**Note:** this overwrites existing files, to create new files:

    for(i in list.files(pattern = "DESeq2_result_*"))
      write.table(read.table(i, col.names = c("gene_id", "baseMean", "log2FC",
                                              "SD", "WaldStatistic", "pval", "padj")),
                  paste0(i, ".fixed.txt"))