[R] multidimensional lists
Evan Cooch
cooch17 at verizon.net
Wed Sep 27 18:07:48 CEST 2006
In the process of moving a number of my scripts from MATLAB -> R, I've
discovered that there is no 'pure' equivalent of MATLAB's cell arrays,
which I use quite often. Basically, I create matrices (as a cell array)
where each element of the matrix is itself a matrix (e.g., 2x2 cell
array where each element of the array is another matrix). I pass these
cell arrays to various functions which then do clever things with the
various matrices (of course) - basically, I need to be able to pass
collections of matrices to functions to do various things, and I need to
be able to control the dimensionality of the cell array to preserve some
structural relationships among the matrices in the array. The cell array
in MATLAB handles this with aplomb.
So far, in R, I've used lists. Given (say) 4 matrices (A,B,C,D), in
MATLAB I can use
test={A,B,C,D} for a row vector cell array, or
test={A;B;C;D} for a column vector cell array.
In R, I get more or less the same thing using
test=list(A,B,C,D)
but this only gives me a row list. For a bunch of technical reasons, I
need to be able to control the orientation (as noted)- this is
especially true for n-dimensional cell arrays. In MATLAB, for example, I
could generate a (say) 2x2 cell array using
test={A B;C D}
The only way I can figure out how to do this in R is using something like
test=list(A,B,C,D);
dim(test) < c(2,2);
This seems to work, but defaults to bycolumn (in other words, instead of
A B
C D
I get
A C
B D
)
So, I follow with
test=t(test) as needed to flip the thing around to byrow.
OK, so the question is - is there a better way? This *seems* to work,
but I'm discovering that R is a lot like working with LaTeX (something I
know much more about) - you can do most things, but there is often a
more elegant way if you can figure out how to find out about it.
Thanks in advance...
More information about the R-help
mailing list