[R] How to: Read Multi-filtes and sort to different files

Carlos J. Gil Bellosta cgb at datanalytics.com
Wed Mar 25 22:53:43 CET 2009

Dear Mr. Li,

To make things simpler, you could place the files corresponding to
different stations in different directories. Then:

1) I would loop over the directories.

2) I would use dir and loop through the resulting vector (that would
contain the file names).

3) I would use read.table with parameters skip (to skip the header) and
the header option set to true.

4) I would aggregate the resulting files in a single big file.

There are ways to do that. Some involve using for loops; you can also
use sapply to loop over files and cbind if you feel confident with a
command similar to

do.call( cbind, sapply( dir(), read.table, skip = 1, header = TRUE ) )

I have not been able to test the expression above and it may not even
parse in R but it is close to something that should work.

Best regards,

Carlos J. Gil Bellosta

On Wed, 2009-03-25 at 14:30 -0700, Qianfeng Li wrote:
> new R user has a question: 
> I have several hundreds of .txt files from  different monitoring sites over several years. 
> (1) different site has a unique name( such as : ST2.20090321.txt = Sation 2  2009 March 21 data, ST3.20090322=Station3, 2009, March 22 data). 
> (2) different site has different file header, but for the same site, the header is the same.
> for example: 
> Sation 2 
> date time wind CO2 
> 2009 10:30 2 3 
> station 3
> data time solar NO
> 2009 10:20 4 5
> Question: 
> How to write a "R" program to read all these files, and combine the data from each station to one file (such as: ST2.master will save all the data from station 2, and ST1.master will save all the data from station 1) ? 
> Thanks a million times!
> Jeff 
> 	[[alternative HTML version deleted]]
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list