Something like this?
> f <- function(ii) {
+ f.frame <- sys.nframe()
+ prev.res <- NA
+ ii.res <- sapply(ii, function(i, ff) {
+ prev.res <- get("prev.res", f=ff)
+ res <- i^2
+ assign("prev.res", res, f=ff)
+ return(c(res, prev.res))
+ }, ff=f.frame)
+ return(ii.res)
+ }
> f(1:5)
[,1] [,2] [,3] [,4] [,5]
[1,] 1 4 9 16 25
[2,] NA 1 4 9 16
>
David Parkhurst wrote:
I'm using splus Enterprise Developer Version 7.0.6 for Microsoft Windows
I'd like to call a function many times using sapply, with the result (a
short vector) from call k being available to call k+1. The results of
the many calls also need to be saved into a matrix (or list?) in the
usual way that sapply does.
How do I save the results of one call for use in the next call? I think
this has to do with frames, but I've never understood those.
Thanks for any help.
Dave Parkhurst
--------------------------------------------------------------------
This message was distributed by s-news@lists.biostat.wustl.edu. To
unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
the BODY of the message: unsubscribe s-news
|