[R] Arrays of variable dimensionality
Jeff Newmiller
jdnewmil at dcn.davis.CA.us
Sat May 30 19:34:25 CEST 2015
Don't.
Arrays in R don't have variable dimensions [1]. To the extent that you pretend otherwise, you will degrade performance and complicate your program. I would argue that similar effects arise in languages that do let you pretend that arrays have variable dimensions.
The main reason variable-sized arrays seem useful is that some algorithms or data sources yield variable amounts of data. There are various ways to handle these cases, and the use of lists to hold intermediate units of data followed by transfer of that data into an array (strategically handled after you know how big the array has to be) is one of the more common ones.
Another technique is to use one of the ragged array or sparse matrix data structures, but they generally work best when the data actually are sparse.
Being more specific about your immediate problem can elicit more specific help.
BTW please post plain text on this list... HTML is not supported by the list reflector and leads to misunderstandings.
[1] More precisely, the total number of elements in the underlying vector is fixed, though you can redefine how the dimensions use the elements in that vector. For example, study the "aperm" function.
---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.
On May 30, 2015 3:29:09 AM PDT, WRAY NICHOLAS <nicholas.wray at ntlworld.com> wrote:
>Hello folks
>
>Supposing I have a multidimensional array in an R prog, say a 4D array.
>
>I want the coordinate quantities to be read off from a vector. The
>values
>in the vector (vec) are generated by a function.
>
>This is easy if the number of dimensions is fixed for both the array
>and
>the number of elements in the vector (say 4):
>
>X<-array[vec[1],vec[2],vec[3],vec[4]]
>
>But if the number of dimensions of the array is not fixed and can
>change
>during the course of the prog I am stuck as to how to do this, as I
>don’t
>know a way of feeding a vector or list of unspecified beforehand length
>into the coordinates for an array
>
>I can’t find anything useful about this on the net
>
>Does anyone have any ideas?
>Thanks, Nick
>
> [[alternative HTML version deleted]]
>
>______________________________________________
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>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.
More information about the R-help
mailing list