[R] writing text and output to file with flexibility

Katharine Mullen kate at few.vu.nl
Sat Aug 30 23:53:07 CEST 2008

you can combine write and write.table, using append=TRUE.


write1 <- function(txt, mat, filename) {
  for(i in 1:length(txt)) {
    write(txt[i], file=filename, append=i!=1)
    write.table(mat[[i]], file=filename, append=TRUE,sep ="\t")

t <- c("text1", "text2", "text3")
m <- list(matrix(0,2,2), matrix(1,2,1), matrix(2,3,2))
write1(t, m, "x.txt")

On Sat, 30 Aug 2008, [ISO-8859-1] Gonçalo Ferraz wrote:

> Hi,
> I want a function to write some of its output into a text file with
> the following format:
> 'some text'
> output matrix A
> 'some more text'
> output matrix B
> 'some other text still'
> output matrix C
> ...
> The dimensions of matrices A, B, ... are different and the total
> number of matrices
> that I want to place in the text file depends on what I pass to the
> function. (I would like to have values
> from adjacent columns separated by tabs.)
> 'write' seems to place only one matrix into one file. 'sink' seems to
> write only what I write, word by word.
> Is there a standard solution for this type of problem?
> Thank you,
> Gonçalo
> ______________________________________________
> 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