Re: [R] modifying argument of a .C call (DUP=FALSE)

From: Peter Dalgaard <p.dalgaard_at_biostat.ku.dk>
Date: Tue 09 Aug 2005 - 05:28:44 EST

Tamas K Papp <tpapp@princeton.edu> writes:

> I have a huge matrix on which I need to do a simple (elementwise)
> transformation. Two of these matrices cannot fit in the memory, so I cannot
> do this in R.
>
> I thought of writing some C code to do this and calling it using .C with
> DUP=FALSE. All I need is a simple for loop that replaces elements with
> their new value, something like
>
> void transform(double *a, int *lengtha) {
> int i;
> for (i=0; i < *lengtha; i++) {
> *(a+i) = calculatenewvaluesomehow(*(a+i))
> }
> }
>
> trans <- function(a) .C("transform",as.double(a), as.integer(length(a))
>
> is it possible to do this? The manuals say that it is dangerous, is it
> possible to avoid the dangers somehow?

It's more a question of whether the dangers affect you. In general, the issue is that you risk modifying a second (virtual) copy of the data along with the one you intend to modify. If you're sure that you don't have any, the point is moot. It is fairly difficult to be sure of that in the general case, which is why we generally discourage DUP=FALSE, especially for package writers, but for personal use you might just get away with it.

-- 
   O__  ---- Peter Dalgaard             ุster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk)                  FAX: (+45) 35327907

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Tue Aug 09 05:36:26 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:39:45 EST