[R] arrays of arrays

Michael Bedward michael.bedward at gmail.com
Wed Nov 10 07:13:59 CET 2010

Hi Sachin,

That's OK - you don't need to know the dimensions up front and you can
add new vectors, or elements to an existing vector, as required.

# empty list to start with
X <- list()

# we get a vector
v1 <- c(1, 2, 3, 4, 5)

# add it to the ragged array
X <- c(X, list(v1))

# get another couple of vectors and add them as well
v2 <- c(9, 8, 7, 6)
v3 <- c(2, 4, 6, 8)
X <- c(X, list(v2, v3))

# add some more elements to the first vector in X - note double
brackets to access
# the vector directly
X[[1]] <- c(X[[1]], 4, 3, 2, 1)


On 10 November 2010 16:46,  <sachinthaka.abeywardana at allianz.com.au> wrote:
> Hi Michael,
> Thanks for that. Its a starting point I guess. But what if I didn't know
> the length of the outer vector is? (i.e. all dimensions are variable). Or
> for that matter I don't actually know what the initial dimensions are going
> to be. All of it is created within a for loop.
> I was hoping for something like a C++ vector, where you can specify
> vector<vector<int>> myarray. I'm sure lists is definitely the way to go,
> but not sure how to implement it.
> Cheers,
> Sachin
> --- Please consider the environment before printing this email ---
> Allianz - Best General Insurance Company of the Year 2010*
> Allianz - General Insurance Company of the Year 2009+
> * Australian Banking and Finance Insurance Awards
> + Australia and New Zealand Insurance Industry Awards
> This email and any attachments has been sent by Allianz Australia Insurance Limited (ABN 15 000 122 850) and is intended solely for the addressee. It is confidential, may contain personal information and may be subject to legal professional privilege. Unauthorised use is strictly prohibited and may be unlawful. If you have received this by mistake, confidentiality and any legal privilege are not waived or lost and we ask that you contact the sender and delete and destroy this and any other copies. In relation to any legal use you may make of the contents of this email, you must ensure that you comply with the Privacy Act (Cth) 1988 and you should note that the contents may be subject to copyright and therefore may not be reproduced, communicated or adapted without the express consent of the owner of the copyright.
> Allianz will not be liable in connection with any data corruption, interruption, delay, computer virus or unauthorised access or amendment to the contents of this email. If this email is a commercial electronic message and you would prefer not to receive further commercial electronic messages from Allianz, please forward a copy of this email to unsubscribe at allianz.com.au with the word unsubscribe in the subject header.

More information about the R-help mailing list