juanjedi
juanjedi

Reputation: 160

Read multiple .txt files in R with scan()

I have a corpus divided into many different .txt files from here: http://www.thegrammarlab.com/?nor-portfolio=corpus-of-presidential-speeches-cops-and-a-clintontrump-corpus

These data files are raw, and so I use the scan function like the following:

scan("Clinton_2016-07-28.txt", what = "character", sep=NULL)

I want to know how I can automate this to scan every file in the folder. Also, I use scan because it creates a character vector which is what i am looking for.

Upvotes: 0

Views: 566

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 389335

As @MrFlick mentioned you can use list.files to get all the text files in working directory and then you can use lapply to read them in a list.

filenames <- list.files(pattern = '\\.txt$')
result <- lapply(filenames, scan, what = "character", sep=NULL)

Upvotes: 1

Related Questions