What is the idiomatic way to collect results in a loop in R if the number of final results is not known beforehand? Here's a toy example:
results = vector('integer') i=1L while (i < bigBigBIGNumber) { if (someCondition(i)) results = c(results, i) i = i+1 } results The problem with this example is that (I assume) it will have quadratic complexity as the vector needs to be re-allocated at every append. (Is this correct?) I'm looking for a solution that avoids this.
I found Filter, but it requires pre-generating 1:bigBigBIGNumber which I want to avoid to conserve memory. (Question: does for (i in 1:N) also pre-generate 1:N and keep it in memory?)
I could make something like a linked list like this:
results = list() i=1L while (i < bigBigBIGNumber) { if (someCondition(i)) results = list(results, i) i = i+1 } unlist(results) (Note that this is not concatenation. It's building a structure like list(list(list(1),2),3), then flattening with unlist.)
Is there a better way than this? What is the idiomatic way that's typically used? (I am very new to R.) I'm looking for suggestion on how to tackle this type of problem. Suggestions both about compact (easy to write) and fast code are most welcome! (But I'd like to focus on fast and memory efficient.)
cfunction is used to extend either vectors or lists. If you can estimate the size then allocating withvector("integer", size)will help reduce the cost of extending.cwithlistwould help here? Unless you preallocate a list, the same problem persists, isn't it?cconcatenates (which triggers a re-allocation of the vector) while withlistI am building a structure likelist(list(list(1),2),3), a linked list. When put in a loop, the latter has linear complexity, the former has a quadratic one. You can easily verify this by a small benchmark: doubling the number of elements to be appended doubles the time withlist, and nearly quadruples it withc. This means that for a "large enough" result, thelistapproach will always be faster. What I did not realize when I wrote the question is that in this case "large enough" ...