Skip to content

Appending elements to an array

3 messages · Jonck van der Kogel, Thomas Lumley, Spencer Graves

#
Hi all,
I am having a bit of trouble with the array structure of R. What I want 
to do is dynamically add/remove elements to an array. For example:
Let's say I have created an array:
 > myArray <- array(c(3,8), dim=c(1,2))
 > myArray
      [,1] [,2]
[1,]    3    8

And I now want to, for example, push an element (5,6) on to this array 
so it will read:
      [,1] [,2]
[1,]    3    8
[2,]    5    6

And then pop the first element of the array so the array now reads:
      [,1] [,2]
[1,]    5    6

How would I do this? So far I've only read how to create an array if 
you know the dimensions beforehand, but I've been unable to find how to 
dynamically add/remove elements to/from an array.

I've figured out how to do this with lists and vectors, but not with 
arrays. For this particular structure I need to work with arrays.

Any help on how to do something like this would be much appreciated.
Thanks, Jonck
#
On Mon, 9 Jun 2003, Jonck van der Kogel wrote:

            
myArray <- array(c(3,8), dim=c(1,2))

#add to the bottom
myArray <- cbind(myArray, c(5,6))

# display the top
myArray
# remove the top
myArray <- myArray[-1,]


	-thomas
#
Have you considered "rbind" to add?  Also, myArray[-2,] will remove row 
2 from myArray.

hope this helps.  spencer graves
Jonck van der Kogel wrote: