From murdoch at stats.uwo.ca  Wed Nov  1 13:54:11 2006
From: murdoch at stats.uwo.ca (Duncan Murdoch)
Date: Wed, 01 Nov 2006 07:54:11 -0500
Subject: [Rd] Subsetting a vector in C
In-Reply-To: <200611010748.07378.sdavis2@mail.nih.gov>
References: <200611010748.07378.sdavis2@mail.nih.gov>
Message-ID: <454898F3.60207@stats.uwo.ca>

On 11/1/2006 7:48 AM, Sean Davis wrote:
> I have a pretty simple question.  I have an array of REALSXP allocated as:
> 
> rsp = allocVector(REALSXP,n)
> 
> and I simply want to pass subsets of rsp of size q (q<<n), for example, to an 
> R function in C.  Is there a way to subset rsp without creating a new REALSXP 
> vector and copying the values for subsetting into it and then passing it to 
> the function?  I'm looking for the R equivalent of rsp[1:q], but in C under 
> the R API.  
> 
> A pointer to source code in the R distribution would be quite helpful, as I 
> haven't found one on my own (at least not one that I recognized as such).

No, there's no way to do that.  R functions can assume they have unique 
access to the objects they work on.  (There are some optimizations which 
mean they sometimes avoid making copies, but the semantics are such that 
they should be able to act as though they have their own copies.  There 
are a couple of exotic exceptions to this rule, but it applies to 
numeric vectors.)

Duncan Murdoch




[Rd] Subsetting a vector in C
Duncan Murdoch