On 07/19/2012 06:11 PM, Bert Gunter wrote:
Hadley et. al:

Indeed. And using a loop is a poor way to do it anyway.

v <- as.list(rep(FALSE,dotot))

is way faster.

-- Bert

I agree that not using a loop is much faster, but I assume that the original question is about the situation where the size of the end result is not known. The values FALSE in the vector/list are just examples of results from a more complex computation. But indeed avoid loops where possible (and often this is possible).

Jan

On Thu, Jul 19, 2012 at 8:50 AM, Hadley Wickham <had...@rice.edu> wrote:
On Thu, Jul 19, 2012 at 8:02 AM, Jan van der Laan <rh...@eoos.dds.nl> wrote:
Johan,

Your 'list' and 'array doubling' code can be written much more efficient.

The following function is faster than your g and easier to read:

g2 <- function(dotot) {
   v <- list()
   for (i in seq_len(dotot)) {
     v[[i]] <- FALSE
   }
}
Except that you don't need to pre-allocate lists...

Hadley

--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

______________________________________________
R-help@r-project.org 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.



______________________________________________
R-help@r-project.org 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.

Reply via email to