[R] Write in file matrices of sifferent size

Steve Lianoglou mailinglist.honeypot at gmail.com
Mon Jul 20 20:56:13 CEST 2009


Hi,

On Jul 20, 2009, at 1:02 PM, julien cuisinier wrote:

> Hi list,
>
> How to save a list content into a text file?
>
> Please consider example below, I have two numeric matrices that I  
> bundle into a list & give each list element a name
>
> Example:
>
>> matrixA <- matrix(0,5,4)
>> matrixB <- matrix(1,7,13)
>> matrixList <- list(matrixA,matrixB)
>
>> names <- c("Matrix A","Matrix B")
>
>> names <- names(matrixList)
>
> How could I get the list in a txt as below:
>
> "
> Matrix A
> (Matrix A element 1,1), (Matrix A element 1,2), ...
> (Matrix A element 2,1), (Matrix A element 2,2), ...
> ...
> Matrix B
> (Matrix B element 1,1), (Matrix B element 1,2), ...
> (Matrix B element 2,1), (Matrix B element 2,2), ...
> ...
> "


I reckon you can do this with two separate calls to write.table using  
(.., append=TRUE).

Not the most elegant, but:

outfile <- open('output.txt', 'w')
cat("Matrix A\n", file=outfile)
close(outfile)

write.table(matrixA, file='output.txt', append=TRUE, ...)

outfile <- open('output.txt', 'a')
cat("\nMatrix B\n", file=outfile)
close(outfile)

write.table(matrixB, file='output.txt', append=TRUE, ...)

Hope that helps,
-steve

--
Steve Lianoglou
Graduate Student: Physiology, Biophysics and Systems Biology
Weill Medical College of Cornell University

Contact Info: http://cbio.mskcc.org/~lianos/contact




More information about the R-help mailing list