[Rd] reshape scaling with large numbers of times/rows

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Aug 24 10:56:37 CEST 2006


On Thu, 24 Aug 2006, Martin Maechler wrote:

> >>>>> "Mitchell" == Mitchell Skinner <mitch at arctur.us>
> >>>>>     on Thu, 24 Aug 2006 00:26:52 -0700 writes:
> 
>     Mitchell> I wrote:
>     >> It still needs some debugging, to put it mildly
>     >> (doesn't work properly on reals), but the basic idea appears to work.
> 
>     Mitchell> It works for reals on a 64-bit machine, but not on
>     Mitchell> a 32-bit machine.  I figure the culprit is this
>     Mitchell> bit of c code:
> 
> 
>     Mitchell> SET_VECTOR_ELT(wideCol, wideRow, VECTOR_ELT(longCol, rowNum));
> 
> 
>     Mitchell> It seems like VECTOR_ELT and/or SET_VECTOR_ELT
>     Mitchell> assumes that the vector elements are word-sized.
> 
> No. It assumes that use what the C API calls 'VECTOR' and R users
> usually call list()s !!!

Since this is R-devel, it might be worth giving a fuller explanation (and 
I had started writing one before seeing Martin's reply).

VECTOR_ELT is used for several types of SEXP, most commonly VECSXP (called 
'list' at R level) but also EXPRSXP ('expression') and WEAKREFSXP ('weak 
reference'), and can also be used for STRSXP ('character vector', but 
STRING_ELT is better).

The R-devel version of R catches such user errors, so it is well be worth 
using for code development.

> Do reread that part in the "Writing R Extensions" and rather
> look at   REAL(.)[.]  and  REAL(.)[] = ..

And on the comment on wanting 'type-agnostic' C code: this is impossible 
as C stores its types differently, and also note that REAL(.)[] is allowed 
but VECTOR_ELT(x,i) = is not.

Another hint: if you find yourself writing REAL(.)[.] repeatedly, rewrite 
it as

double *ra = REAL(a);

ra[i] = ...

since REAL(a) is a function call and can add appreciably to the execution 
time if used inside a loop.  (This is not true for internal code in R, 
BTW, where REAL is a macro.)

And yes, I know there are examples like this in "Writing R Extensions" in 
R 2.3.1, so this is another reason to use 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



More information about the R-devel mailing list