[R] Write in file matrices of sifferent size

jim holtman jholtman at gmail.com
Mon Jul 20 20:59:36 CEST 2009


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?




More information about the R-help mailing list