I have a routine that generates a series of data vectors, one iteration at a time. I would like to find a way to “grow” either a list or a matrix that holds these vectors. I tried to create a list,
PROTECT( myList = allocVector( VECSXP, 1 ) )
But is there a way to grow the list, by pushing a vector element in the end?
Also, I wouldn’t mind using a matrix, since the vectors I generate are of the same length.
Rf_lengthgetsinRinternals.h; implemented inbuiltin.c:lengthgets. The returned pointer needs to be PROTECTed, so one pattern isIf one were growing a list based on some unknown stopping condition, the approach might be like in R, with pre-allocate and fill followed by extension; the following is psuedo-code:
This performs a deep copy of
myList, so can become expensive and in some ways if ht emain objective to evaluate some_calculation, then it seems like it’s easier and not too much less efficient to do the pre-allocate and extend operations in an R loop, calling some_calculation and doing assignment inside the loop.