[R] stack operations on vectors

Aidan Dysart adysart at nonlineardynamics.com
Thu Apr 20 19:08:41 CEST 2000


It would be nie to have some basic stack operations for simple vectors and
lists. ie:

> a <- c(1,2,3,4,5)
> b <- pop(a)
> a
[1] 1 2 3 4
>b
[1] 5
> push(b, 6)
> b
[1] 5 6

notice how pop and push modify the original vector and not local copy
inside the functions environment. Is there any way to do this? I tried
this:

push <- function(vec,val) {
  vec <<- c(vec,val)
}

pop <- function(vec) {
  l <- length(vec)
  val <- vec[l]
  vec <<- vec[1:(l-1)]
  val
}

but those only create the object 'vec' in the global environment. is there
a way to pass an object by reference?

It seems like the only way to do this would be to have it call an
internal function.

PS. please include me in the reply, and I am only on the R-Help digest.
thanks.

-- 
Aidan Dysart | adysart at nonlineardynamics.com | www.nonlineardynamics.com

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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