I have 50 text files all beginning with NEW. I want to loop through each textfile/dataframe and run the same functions for each of these files and then output the results via the write.table function. Therefore, for each 50 files, the same functions are applied and then 50 independent output results should be created containing the original name with word 'output' at the end.
Here is my code. I have used the list.file function to read in the 50 files but how can I adapt my code below so that each of the below R commands/functions run for each of the 50 files independently and then output the corresponding 50 files?
file_list <- list.files("/data/genome/relevantfiles/AL/*NEW*.") #reading in the list of 50 files in my directory all starting with NEW
df <- file_list #each file *NEW* is a df #this does not work - how can apply each file into a dataframe.
##code for each dataframe. The code/function is shown more simply below because it works
#running a function on the dataframe
df_output_results <- coloc.susie(dataset1 = list(beta=df$BETA, varbeta=df$Varbeta, N=1597, type="quant" ...)
#printing out results for each dataframe
final_results <- print(df_output_results$summary)
#outputting results
write.table(final_results, file = paste0(fileName,"_output.txt"), quote = F, sep = "\t")
I am unsure how to adapt the code so that each file is inputted in a list and the following R codes are applied to each file in the code block and then outputted into a seperate file ... repeated for 50 files. I am assuming I need to use lapply but not sure how to use this? The codes in the code block work so there is not issue with the codes.