[R] Write to multiple connections or multiple text files

Andersson, Jafet Jafet.Andersson at eawag.ch
Fri Jan 23 14:06:03 CET 2009


Hi all,

I want to modify a large number of text files (ca 4000) by replacing a
value found on a particular line in them with a value from an R object.

For a single file I would normally use: 
	con<-file ("foo.txt", open="r+")			
	content<-readLines(con)
	content[n]<-"test"
	writeLines(content,con)
	close(con)

For repeating this for several files I can write a for loop around this.
However, my problem with this is that it is rather slow. I am therefore
wondering if there is any other way to write to multiple connections in
a similar way as one can e.g. write to a large number of rows in a
matrix simultaneously? 

(Note that seek() is not so practical for me since the number of bytes
before the specific line varies between the files, therefore I use
reanLines() and match the right line instead.)

My Systems:
OS: Windows Server 2003 & Linux Red Hat (interchangeably)
R version: 2.7.2

Thanks for any suggestions!

ooo
Jafet Andersson
Eawag - The Swiss Federal Institute of Aquatic Science and Technology
Ueberlandstrasse 133
P.O. Box 611
CH-8600 Duebendorf
Switzerland
Phone: +41 (0)44 823 5358
Fax:     +41 (0)44 823 5028
http://www.eawag.ch/index_EN




More information about the R-help mailing list