Reading multiple TXT files

I have a folder named "EpiRegio" that has 4228 txt files. How Can I read all txt files and store each of them in a specific dataframe?

I wrote this line of code for reading one file. It worked:

df1 = read.delim("ENSG00000000457.txt", header = TRUE, sep = "\t")

But How can I write the code to get each of them and store them in different dataframes?

This code should work for your goal. My files are stored in a directory named FILES that is a subdiretory of the working directory.
It might be better to load all of the data frames into a list where it would be easier to iterate over them.

TxtFiles <- list.files(path = "FILES",pattern = "txt$")
MyFunc <- function(Nm) {
  FullPath <- paste("FILES", Nm, sep="/")
  DFname <- sub("\\.txt", "", Nm) #drop .txt from Nm
  DF <- read.delim(FullPath, header = TRUE, sep = "\t")
  assign(DFname, DF, envir = .GlobalEnv)
}
library(purrr)
walk(TxtFiles, MyFunc)
1 Like

Thank you.
I have one more question, is there a way to remove the incorrect files or empty files?
Actually, I wrote the below code and it worked. but if there is an incorrect file it would show an error(for the first line).

fileList <- list.files(path="~/Desktop/ project/EpiRegio", pattern=".txt")
sapply(fileList, read.delim)
mainlist = list()
for (i in 1: length(fileList)) {
  mainlist[[i]] = read.delim(fileList[i], header = TRUE, sep = "\t")
}
1 Like

Are you seeing an error returned by list.files()?

yes, for one of the txt files.

In my code you can wrap the read.delim in a try() that will catch the error. You will still see the error in the console but the code will continue.

TxtFiles <- list.files(path = "FILES",pattern = "txt$")
MyFunc <- function(Nm) {
  FullPath <- paste("FILES", Nm, sep="/")
  DFname <- sub("\\.txt", "", Nm) #drop .txt from Nm
  SUCCESS <- try(
  DF <- read.delim(FullPath, header = TRUE, sep = "\t")
  )
  if(!inherits(SUCCESS, "try-error")) {
    assign(DFname, DF, envir = .GlobalEnv)
  }
}
library(purrr)
walk(TxtFiles, MyFunc)
3 Likes

In my last post I was fixing the error thrown wehn reading an empty file. The error was not thrown by list.files but by read.delim. Can you please post your exact coe and the text of the error you see?

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.