-2

I have a set of 30 files with common root names (e.g. file-0001, file-0002 etc) with identical variable names (all files in the same directory) that I want to append to a master data set. How do I do that?

msoderstrom
  • 537
  • 1
  • 7
  • 14

3 Answers3

6

You could try (if all the files are in the same working directory)

 files <- list.files(pattern="file-[0-9]+")

 res1 <- do.call(`rbind`,lapply(files, read.table, header=T))

Or

 library(data.table) 
 rbindlist(lapply(files, fread))
akrun
  • 874,273
  • 37
  • 540
  • 662
2

Use list.files to get all the files in a certain folder following a certain pattern e.g. :

DF <- masterDF
filePaths <- list.files(path="folderpath",pattern="file-[0-9]+",include.dirs=T)
for(filePath in filePaths){
  currentDF <- read.table(filePath)
  DF <- rbind(DF, currentDF)
}
digEmAll
  • 56,430
  • 9
  • 115
  • 140
-1

Make a table with the name of all your files and an id, then write something like this:

#load the table with the name of the file & id
list<-read.table("Table.txt",header=T)

#define the range
i=max(list$id)+1

#make the loop
while (i>0)
i=i-1

#select the name of your file from the table
for(i in 1:length(list[,1])) {if (list[i,1]==id) {file<-list[i,2]} }
file

#set work directory
setwd(your directory)

#use your function
result<-rbind(file)

change the algorithm to your own work!

Pau
  • 125
  • 6