[R] Output multiple files

David Winsemius dwinsemius at comcast.net
Mon Dec 7 19:03:25 CET 2009


On Dec 7, 2009, at 12:42 PM, Muhammad Rahiz wrote:

> Hello,
>
> I want the ouput to take the name of the original file. I have the  
> following code - but the problem is that the output overrides the  
> original file which I desire not.
>
> So given an input file called File1.txt, I want the ouput to be  
> called File1.out.txt, or something along this line. I'm working with  
> many files, fyi.
>
> out=c("file1.txt", "file2.txt", "file3.txt")

Take a look at this:

 > paste(strsplit(out[1], "\\.")[[1]][1], "out.txt", sep=".")
[1] "file1.out.txt"

> for (i in 1:3){               # where 3 is the number of files
> file[[i]] <- file[[i]] * 3   # just a simple calculation to multiply  
> the matrix by 3
> write.table(file[[i]],out[i], row.names=FALSE, col.names=FALSE)

perhaps something like this (untested in absence of data) :

write.table(file[[i]],file=paste(strsplit(out[i], "\\.")[[1]][1],  
"out.txt", sep="."), row.names=FALSE, col.names=FALSE)
}

>
> Thanks.
>
> -- 
> Muhammad Rahiz  |  Doctoral Student in Regional Climate Modeling					
> Climate Research Laboratory, School of Geography & the Environment	
> Oxford University Centre for the Environment
> South Parks Road, Oxford, OX1 3QY, United Kingdom Tel: +44  
> (0)1865-285194	 Mobile: +44 (0)7854-625974
> Email: muhammad.rahiz at ouce.ox.ac.uk
>
> ______________________________________________
> 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.

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list