[R] multidimensional lists
Prof Brian Ripley
ripley at stats.ox.ac.uk
Wed Sep 27 18:33:26 CEST 2006
matrix(some_list, nr, nc, byrow=TRUE) may be what you are looking for.
R arrays can be of any vector type, including list. I'd get used to R's
Fortran ordering rather than force transposes all the time.
On Wed, 27 Sep 2006, Evan Cooch wrote:
> 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...
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-help
mailing list