From P.Dalgaard at biostat.ku.dk  Fri Oct 19 12:19:20 2007
From: P.Dalgaard at biostat.ku.dk (Peter Dalgaard)
Date: Fri, 19 Oct 2007 12:19:20 +0200
Subject: [Rd] append/concatenate an element to a list in C-language
In-Reply-To: <1192784927.8051.33.camel@llull.imim.es>
References: <1192729299.17964.27.camel@llull.imim.es>
	<Pine.LNX.4.64.0710181848020.3360@gannet.stats.ox.ac.uk>
	<1192784927.8051.33.camel@llull.imim.es>
Message-ID: <471884A8.7070105@biostat.ku.dk>

Robert Castelo wrote:
> hi,
>
> thanks to all the replies, i think the discussion can be followed
> throughout this message.
>
>   
>> You are returning an result in a function that returns void: the compiler 
>> will complain at you.
>>     
>
> apologies, indeed it should have been
>
> SEXP f(SEXP list, SEXP element);
>
>   
>> If you study the R Internals manual you will see that there is no space on 
>> the original VECSXP for another element, so you do *need* to create a new 
>> VECSXP.  Note that creating a new list does not mean necessarily copying 
>> the elements, but you do need to think about the NAMED bits.
>>     
>
> in this list i need not names associated to each element, i guess that
> should release me from thinking about the NAMED bits you refer to (?).
>
>   
No! This is very important: The same R object may be known under
multiple names and this is what NAMED records (well, it tries).  When
this is the case, it is not safe to modify it destructively.  E.g.

x <- rnorm(100)
y <- x     # at this point y and x refer to the SAME object
y[1] <- 0  # NOW we have to duplicate y or the subassignment will change x

Notice that this appears in disguised forms:

g <- function(y) {
   y[1] <- 0  # MAY require duplication
   mean(y)
}
x <- rnorm(100)
g(x) # duplication needed
g(rnorm(100)) # duplication not needed

Don't try anything involving destructive modification of objects before
you have understood this!

-- 
   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 at biostat.ku.dk)                  FAX: (+45) 35327907



[Rd] append/concatenate an element to a list in C-language
Peter Dalgaard