How to import multiple text files based on titles using R?

591 views Asked by At

I'm using the 'readtext' package to import multiple text files in a specific directory.

library(readtext)
DATA_DIR <- system.file("extdata/", package = "readtext")
readtext(paste0(DATA_DIR, "/txt/UDHR/*"))

My question is: is there any way to import text files based on their titles? I want to import files with titles containing a specific word, for example, apple.

Thank you for your help in advance.

1

There are 1 answers

0
ASH On

This should do what you want.

# List all txt files including sub-folders
list_of_files <- list.files(path = "C:\\your_path_here\\", recursive = TRUE,
                            pattern = "the_run", full.names = TRUE)

library(data.table)

# Read all the files and create a FileName column to store filenames
DT <- rbindlist( sapply(list_of_files, fread, simplify = FALSE),
                 use.names = TRUE, idcol = "FileName" )

In this silly example, I setup one parent folder which contains 3 sub-folders. In each sub-folder I have 5 text files: the_run1.txt, the_run2.txt, the_run3.txt, run1.txt, and run2.txt. I am hitting the parent folder and looking through all sub-folders for the text files that have 'the_run' in the name of the file. That's about it. I created a list of these 9 files (3 files in 3 folders), and looped through this list to load everything into a single data table.