[R] sequencially merge multiple files in a folder

Richard.Cotton at hsl.gov.uk Richard.Cotton at hsl.gov.uk
Tue Nov 18 09:39:26 CET 2008

> Here I have a folder with more than 50 tab-delimited files. Each 
> file has a few hundreds of  thousands rows/subjects, and the number 
> of columns/variables of each file varies.The 1st row consists of all
> the variable names.
> Now I would like to merge all the files into one tab-delimited file 
> by a common column named "Ident"
> Is there any good way to sequencially merge all of them together?
> Here when I say  "sequencially" I mean merging file_1  and file_2 
> first and then merge the resulting data frame and file_3, and keep 
> going on and on till all files are merged.

Read each of the tab-delimited files into R using read.delim

dfr1 <- read.delim("file1.txt")
dfr2 <- read.delim("file2.txt")
dfr3 <- read.delim("file3.txt")

(If the files are nicely named then you could do this in a loop.)

It's not entirely clear what you want to do next, since you haven't 
provided an example of your data.  Either

1. Use rbind to concatenate the data frames if each frame has the same 

masterdfr <- rbind(dfr1, dfr2)
masterdfr <- rbind(masterdfr, dfr3)

2. Use merge to merge the data frames if they have different columns

masterdfr <- merge(dfr1, dfr2)    #you'll neeed to specify some other 

If you want a clearer answer, you'll have to read the posting guide and 
provide more details about your data.


Mathematical Sciences Unit


This message contains privileged and confidential inform...{{dropped:20}}

More information about the R-help mailing list