[R] stack operations on vectors

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Thu Apr 20 22:14:42 CEST 2000


Thomas Lumley <thomas at biostat.washington.edu> writes:

> There are two ways to do this sort of thing.  One is similar to what you
> were doing, but is, I think, considered harmful (messing with frames
> should be restricted to places where it is essential).

Actually, I think it *is* essential when you're trying to simulate
call-by-reference semantics, so code like the below would be quite OK.

> a <- c(1,2,3,4,5)
> pop<-function(x) eval(substitute(x<-x[-1]),parent.frame())
> > a
> [1] 1 2 3 4 5
> > pop(a)
> > a
> [1] 2 3 4 5

Um, you seem to have forgotten to return the old value of x[1]...

Exercise: Will this work, and why not?

pop<-function(x) {eval(substitute(x<-x[-1]),parent.frame());x[1]}

How about this:

pop<-function(x) if (length(x)>0)
		{eval(substitute(x<-x[-1]),parent.frame());x[1]} else NA

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list