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

From: Peter Dalgaard <P.Dalgaard_at_biostat.ku.dk>
Date: Fri, 19 Oct 2007 12:19:20 +0200

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

______________________________________________
R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Fri 19 Oct 2007 - 10:22:35 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 25 Oct 2007 - 11:37:11 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.