Michael
Michael

Reputation: 575

Read all files containing specific word in R

I have 10 files in a folder named:

data_2008.txt

...

data_2017.txt

Instead of reading the data like this:

read.table(myfolder\data_2008.txt)

...

read.table(myfolder\data_2017.txt)

Is there a smart way where I can read all the files containing the name "data_" in R?

Upvotes: 0

Views: 348

Answers (1)

Limey
Limey

Reputation: 12585

Untested code

fileList <- list.files(path="myFolder", pattern="data_\\d{4}\\.txt")
tableList <- lapply(fileList, read.table)

Upvotes: 1

Related Questions