Re: [Rd] Shrinking a List

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Thu, 11 Mar 2010 06:27:11 +0000 (GMT)

And how would you do this at R level?

I would use length(A) <- M, and the corresponding C function is lengthgets.

Of course, the wasteful part is to create a far-too-long vector in the first place, rather than growing it as needed.

On Wed, 10 Mar 2010, Saptarshi Guha wrote:

> Hello,
> I create a VECSXP(call it A) with size N(~ 5000), i then proceed to
> fill the elements and find out I don't need to fill more than
> M (M<< N).
> Thus if i return A to the user's R code, he/she will see a list of
> length 5K of which N-M are NULLs.
>
> To avoid this, I create a new VECSXP B of length M and /duplicate/ the
> elements of A.
>
> Since I do this often, it appears to be wasteful, so can I
>
> a) is there a resize function for a VECSXP?
>
> if not
>
> b) can i just do something like
>
> SET_VECTOR_ELT(B,i, VECTOR_ELT(A,i)) instead of wrapping
> VECTOR_ELT(A,i) inside a call to Rf_duplicate
> (i will be UNPROTECTING A and B, though I will return B)
>
>
> I suppose I can do (b), since B (since it is being returned) is
> automatically protected and therefore all its elements will also be
> protected, correct?
>
>
> Thank you
> Saptarshi
>
> ______________________________________________
> R-devel_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

-- 
Brian D. Ripley,                  ripley_at_stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Thu 11 Mar 2010 - 06:30:57 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 11 Mar 2010 - 14:01:00 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive