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

As I understood, you want to open some files containing the pattern "DESeq2_result*" as data.frames in a list, assigning new names to the columns of these dataframes based on the `a` vector, and maybe name the elements of the list based on the files name? You can do it that way:


    filelist <- list.files(pattern = "DESeq2_result")
    
    a <- c("gene_id", "baseMean", "log2FC",
           "SD", "WaldStatistic", "pval", "padj")
    
    data<-list()
    
    for (i in 1:length(filelist)) {
      data[[i]]<-read.table(filelist[i])
      colnames(data[[i]])<-a
    }

    names(data)<-filelist

    for (i in 1:length(data)) {
        output_name <- paste0("colnames_",names(data[i]))
        write.table(data[[i]], output_name, quote = F, row.names = F, col.names = T, sep="\t")
    }


EDIT: added the write.table part to take your comment into account in order to export the imported files with the column names.
