[R] How to create a list that grows automatically

Alberto Monteiro albmont at centroin.com.br
Fri Mar 9 17:56:56 CET 2007


Young-Jin Lee asked:
> 
> I would like to know if there is a way to create a list or an array (or
> anything) which grows automatically as more elements are put into 
> it. 
>
???

I think this is the default behaviour of R arrays:

x <- vector(length=0)  # create a vector of zero length
x[1] <- 2
x[10] <- 3
x[length(x) + 1] <- 4
x # 2 NA NA ... 3 4


> What I want to find is something equivalent to an ArrayList 
> object of Java language. In Java, I can do the following thing:
> 
> // Java code
> ArrayList myArray = new ArrayList();
> myArray.add("object1");
> myArray.add("object2");
> ....
> // End of java code
> 
myArray <- vector(length=0)
myArray <- c(myArray, "object1")
myArray <- c(myArray, "object2")
myArray # array with 2 strings

Alberto Monteiro



More information about the R-help mailing list