[R] Appending elements to an array
Thomas Lumley
tlumley at u.washington.edu
Mon Jun 9 23:01:58 CEST 2003
On Mon, 9 Jun 2003, Jonck van der Kogel wrote:
> 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.
>
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
More information about the R-help
mailing list