[R] array of variable length vectors
B.Rowlingson at lancaster.ac.uk
Mon Feb 2 12:22:09 CET 2004
Giampiero Salvi wrote:
> I'd like to store N vectors of different lengths, and to be able to
> access them with an index, and eventually free the memory for one
> of them without modifying the indexes to the others.
> int *pv;
> pv = (int *) malloc(13 * sizeof(int));
> pv = (int *) malloc(7 * sizeof(int));
> pv = (int *) malloc(110 * sizeof(int));
> What is the best data type (or class) in R to do such a thing?
A list, with vector elements (index starts at 1 in R):
pv = list()
pv[] = real(13)
pv[] = real(7)
pv[] = real(110)
then the equivalent of freeing the memory and keeping the indexing
pv[] = real(0)
pv[] = NULL (which deletes element 2)
*BUT* I dont know if R will really free() the memory at that point.
You may need to force the garbage collection with gc()....
More information about the R-help