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

Peter Dalgaard P.Dalgaard at biostat.ku.dk
Fri Oct 19 12:19:20 CEST 2007


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



More information about the R-devel mailing list