r - Collecting an unknown number of results in a loop -
what idiomatic way collect results in loop in r if number of final results not known beforehand? here's toy example:
results = vector('integer') i=1l while (i < bigbigbignumber) { if (somecondition(i)) results = c(results, i) = i+1 } results
the problem example (i assume) have quadratic complexity vector needs re-allocated @ every append. (is correct?) i'm looking solution avoids this.
i found filter
, requires pre-generating 1:bigbigbignumber
want avoid conserve memory. (question: for (i in 1:n)
pre-generate 1:n
, keep in memory?)
i make linked list this:
results = list() i=1l while (i < bigbigbignumber) { if (somecondition(i)) results = list(results, i) = i+1 } unlist(results)
(note not concatenation. it's building structure list(list(list(1),2),3)
, flattening unlist
.)
is there better way this? idiomatic way that's typically used? (i new r.) i'm looking suggestion on how tackle type of problem. suggestions both compact (easy write) , fast code welcome! (but i'd focus on fast , memory efficient.)
here algorithm doubles size of output list fills up, achieving linear computation times show benchmark tests:
test <- function(bigbigbignumber = 1000) { n <- 10l results <- vector("list", n) m <- 0l <- 1l while (i < bigbigbignumber) { if (runif(1) > 0.5) { m <- m + 1l results[[m]] <- if (m == n) { results <- c(results, vector("list", n)) n <- n * 2l } } = + 1l } unlist(results) } system.time(test(1000)) # user system elapsed # 0.008 0.000 0.008 system.time(test(10000)) # user system elapsed # 0.090 0.002 0.093 system.time(test(100000)) # user system elapsed # 0.885 0.051 0.936 system.time(test(1000000)) # user system elapsed # 9.428 0.339 9.776
Comments
Post a Comment