I wrote a loop which inputs several text files, performs a few functions on each and combines them. I've copied it below and annotated each line. However, the first file in i gets read in (and added to my final table) twice! Also, looking forward to streamline this loop.
source_files<-list.files(pattern="_output.txt") # This line finds all file ending with .txt
source_files from above lists the appropriate files to input in the below loop.
for (i in source_files){
    if (!exists("final_table")){
        df_import<-read.table(i, header=FALSE, sep="\t") # reads in each file
        names<-unlist(strsplit(i,"_")) # reformats input file name and parses to 'names'
        df_import$Sample<-names[1] # replaces col[1] header with first part of file name
        df_import$DB<-names[2] # replaces col[1] header with first part of file name
        final_table<-df_import # creates the final table data frame
        rm(df_import) # remove excess df
        }
    if (exists("final_table")){
        df_import<-read.table(i, header=FALSE, sep="\t") # reads in each file
        names<-unlist(strsplit(i,"_")) # reformats input file name and parses to 'names'
        df_import$Sample<-names[1] # replaces col[1] header with first part of file name
        df_import$DB<-names[2] # replaces col[1] header with first part of file name
        final_table <-rbind(final_table, df_import) # Adds to existing final table
        rm(df_import)   
    }
}
This loop is working great, except that final_table has a duplication - any suggestions?
 
     
    