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), ... ... " Write or write.table or write.csv did not help as it complains the matrices are not of equal size Any help/direction appreciated many thanks regards, Julien Cuisinier " _________________________________________________________________ [[elided Hotmail spam]] [[alternative HTML version deleted]]
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
use capture.output> matrixA <- matrix(0,5,4) > matrixB <- matrix(1,7,13) > matrixList <- list(matrixA,matrixB) > names(matrixList) <- c("matrixA", "matrixB") > matrixList$matrixA [,1] [,2] [,3] [,4] [1,] 0 0 0 0 [2,] 0 0 0 0 [3,] 0 0 0 0 [4,] 0 0 0 0 [5,] 0 0 0 0 $matrixB [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [1,] 1 1 1 1 1 1 1 1 1 1 1 1 1 [2,] 1 1 1 1 1 1 1 1 1 1 1 1 1 [3,] 1 1 1 1 1 1 1 1 1 1 1 1 1 [4,] 1 1 1 1 1 1 1 1 1 1 1 1 1 [5,] 1 1 1 1 1 1 1 1 1 1 1 1 1 [6,] 1 1 1 1 1 1 1 1 1 1 1 1 1 [7,] 1 1 1 1 1 1 1 1 1 1 1 1 1> capture.output(matrixList, file='myMatrix.txt')#here is the text file created: $matrixA [,1] [,2] [,3] [,4] [1,] 0 0 0 0 [2,] 0 0 0 0 [3,] 0 0 0 0 [4,] 0 0 0 0 [5,] 0 0 0 0 $matrixB [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [1,] 1 1 1 1 1 1 1 1 1 1 1 1 1 [2,] 1 1 1 1 1 1 1 1 1 1 1 1 1 [3,] 1 1 1 1 1 1 1 1 1 1 1 1 1 [4,] 1 1 1 1 1 1 1 1 1 1 1 1 1 [5,] 1 1 1 1 1 1 1 1 1 1 1 1 1 [6,] 1 1 1 1 1 1 1 1 1 1 1 1 1 [7,] 1 1 1 1 1 1 1 1 1 1 1 1 1 On Mon, Jul 20, 2009 at 1:02 PM, julien cuisinier<j_cuisinier at hotmail.com> 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), ... > > ... > > " > > > > > > Write or write.table or write.csv did not help as it complains the matrices are not of equal size > > > > Any help/direction appreciated > > > > > > many thanks > > > > regards, > > Julien Cuisinier > > > > > > > > > > > > " > > > > _________________________________________________________________ > [[elided Hotmail spam]] > > ? ? ? ?[[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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
Dear Julien, Try sink(): matrixA <- matrix(0,5,4) matrixB <- matrix(1,7,13) matrixList <- list(matrixA,matrixB) names(matrixList) <- c("matrixA", "matrixB") sink("yourname.txt") matrixList sink() See ?sink for more information. HTH, Jorge On Mon, Jul 20, 2009 at 1:02 PM, julien cuisinier <j_cuisinier@hotmail.com>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), ... > > ... > > " > > > > > > Write or write.table or write.csv did not help as it complains the matrices > are not of equal size > > > > Any help/direction appreciated > > > > > > many thanks > > > > regards, > > Julien Cuisinier > > > > > > > > > > > > " > > > > _________________________________________________________________ > [[elided Hotmail spam]] > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]