s-news
[Top] [All Lists]

Re: Saving vector betwen function calls in sapply

To: David Parkhurst <parkhurs@imap.iu.edu>
Subject: Re: Saving vector betwen function calls in sapply
From: Tony Plate <tplate@blackmesacapital.com>
Date: Fri, 27 Jan 2006 17:04:30 -0700
Cc: S-News <s-news@lists.biostat.wustl.edu>
In-reply-to: <43DAA34B.4010201@imap.iu.edu>
References: <43DAA34B.4010201@imap.iu.edu>
User-agent: Mozilla Thunderbird 1.0.5 (Windows/20050711)
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



<Prev in Thread] Current Thread [Next in Thread>