[R] storing matrices in a list or vector and preserve dimensions
Bram Kuijper
a.l.w.kuijper at rug.nl
Tue Jan 8 15:11:07 CET 2008
Hi all,
As an R newbie, I wonder how I can store multiple matrices in a list()
or vector() object without losing their structure. I should be able to
retrieve the matrix from the list later on.
If I just append() the matrices to a list() object, they automatically
lose their dimensions, whereas I would like to preserve the dimensions
of the matrices.
Is there any function in R which allows me to store a _fully preserved_
object inside a list() or vector() structure?
thanks in advance,
Bram Kuijper
PS: this is what I do:
# two matrices, which are my objects to be put in the list
my_matrix_object1 <- matrix(data=rep(1,times=9),nrow=3,ncol=3);
my_matrix_object2 <- matrix(data=rep(0,times=9),nrow=3,ncol=3);
my_list <- list()
# note that I explicitly want to append the object to the list
# instead of directly inserting it from the beginning
# (e.g., code to be used in a for-loop)
my_list <- append(my_list,as.matrix(my_matrix_object1));
my_list <- append(my_list,as.matrix(my_matrix_object2));
However, this results in the following list structure...
[[1]]
[1] 1
[[2]]
[1] 1
[[3]]
[1] 1
... etcetera,
whereas I just want to preserve the matrix structure in my list:
my_matrix_object1;
[,1] [,2] [,3]
[1,] 1 1 1
[2,] 1 1 1
[3,] 1 1 1
,
[,1] [,2] [,3]
[1,] 0 0 0
[2,] 0 0 0
[3,] 0 0 0
I guess I need a more sophisticated function then append, but I do not
know which one?
More information about the R-help
mailing list