[R] How to create a list that grows automatically

bogdan romocea br44114 at gmail.com
Fri Mar 9 18:28:57 CET 2007


This is a bad idea as it can greatly slow things down (the details
were discussed several times on this list). What you want to do is
define from the start the length of your vector/list, then grow it (by
a large margin) only if it becomes full.
lst <- vector(mode="list", length=100000)  #assuming 100k nodes are enough
#populate the list, then remove the unused nodes if you care to
lst <- lst[sapply(lst, function(x) {!is.null(x)})]


> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Young-Jin Lee
> Sent: Friday, March 09, 2007 11:08 AM
> To: r-help
> Subject: [R] How to create a list that grows automatically
>
> Dear R users
>
> 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. 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
>
> Thanks in advance.
>
> Young-Jin Lee
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> 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