0

I have a folder which contains .txt files. The folder is called 'joined_data'. I want to read all the files into R so that they keep appending to the end of a single data frame called 'data'. That way all the data is combined into one data frame. I would like this to be automated.

I am currently trying to run this in a loop as shown below. However, it only keeps the last file in the data frame and deletes the rest.

library(here)
here()

#create list of files
files <- list.files(path=here('joined_data'),pattern = "\\.txt$")

## Iterates over the length of list
for (m in 1:length(files)){
  data <- read.table(here('joined_data',paste('Chunk_',m,'_all.txt',sep='')), sep='\t', header=T, append = TRUE)
}
Luke Jenner
  • 129
  • 5
  • 1
    You're overwriting `data` at each iteration of your loop. I would convert your `for` loop to `lapply` and then use `dplyr:;bind_rows()` or `rbind()`. – Limey Aug 02 '22 at 10:06
  • 2
    This question is already answered here [https://stackoverflow.com/questions/3397885/how-do-you-read-multiple-txt-files-into-r] – Antreas Stefopoulos Aug 02 '22 at 10:08

0 Answers0