[Rd] Get the tail of a list in C

romain at r-enthusiasts.com romain at r-enthusiasts.com
Fri Jul 5 15:43:27 CEST 2013

Le 2013-07-05 07:15, maxpar a écrit :
> Hi,
> I am write R extensions in C. Now I have a VECSXP variable, so how 
> can I get
> the tail of it (all but the first one) as a new VECSXP. I tried 
> CDR(), but
> it gives error.
> Thanks.


A VECSXP is actually an array of pointers, not a linked list. If you 
want the tail, you have to allocate new data.
For a simplistic version that does not deal with attributes, names, etc 
..., try something like this:

int n = length( x ) - 1  ;
SEXP taildata = PROTECT( allocVector( VECSXP, n ) ) ;
for( int i=0; i<n; i++)
     SET_VECTOR_ELT( taildata, i, VECTOR_ELT( x, i ) ) ;


More information about the R-devel mailing list