[R] function design

Michaell Taylor pols1oh at bestweb.net
Tue Mar 19 14:38:57 CET 2002

```Thanks so very much for your time.  I am impressed by R, but tend to
think in stata/sas (but I am making progress). I am even more impressed
by participants in this list.

On Tue, 2002-03-19 at 07:31, Robert Gentleman wrote:
> Michaell,
>   I think this is what you want to do,
> lagger <- function(X, y, z, l=1) {
>    lenX <- length(X)
>    temp <- c(rep(NA,l), X[-((lenX-(l-1)):lengX)] )
>    temp[y==min(y)] <- z
>    return(temp)
> }

ah, I guess this change is for a bit more speed and readibility. Thanks.

>
> growit <- function(x,y,z) (x/lagger(x,y,z))
>
> #what's data? it must be a free variable and
> #that is not generally a good idean

I am not  entirely sure what you mean by "free" variable but it is a
data.frame.

>
> inrange <- function(v, Y, s, z, mlv, met) {
>         lower <- mean(v[data\$yr==Y])-(var(v[data\$yr==Y])^.5)
>         upper <- mean(v[data\$yr==Y])+(var(v[data\$yr==Y])^.5)
>         MLE <- mlv[ML\$yr==Y & ML\$metcode==met]
>         diffa <- 0
>         if (MLE<lower) {
>                 print('lower')
>                 diffa <- lower-MLE
>                 rval <- v[data\$yr==Y]-diffa
>         }
>         if (MLE>upper) {
>                 print('higher')
>                 diffa <- MLE-upper
>                 rval <- v[data\$yr==Y]+diffa
>         }
>         if (diffa!=0) {
>                 v <- growit(v,s,z)
>                 }
>          rval
>         }
>
> #note that you assign something
> data\$Linv[data\$yr==Y] <- inrange(data\$Linv,Y,data\$yr,starting.inv,ML\$inv,met)
>
> Also, why if you pass in data\$yr as variable 's' do you not use it?
> In lines 1,2,8,11, for example.

's' is passed by inrange to growit. poor planning, but 's' in inrange
converts to 'y' in growit.

> Why are you changing adjustment and not returning it?

a bug that I had not seen yet.

>
> Basically, you have no need to have a function change anything outside
> of its scope. Simply return an object and assign is as needed in the
> calling functions (sort of the whole point really).
> If you need to return two things use a list.
>
> return(list(a=1,b=10), for example

Now THIS is interesting.  I had thought that a multiple return would be
neat, but didn't realize one could do it.  This function was built to
change in place values for the data frames data and adjustment with the
notion that a multiple return was not possible.

Also, because the data is somewhat large, I thought that perhaps an "in
place" change to vectors in the data data.frame might be more memory
conservative than a "return" sort of arrangement.  This probably is not
the case.

Thanks again.

Michaell
>
>
> robert
>
> --
> +---------------------------------------------------------------------------+
> | Robert Gentleman                 phone : (617) 632-5250                   |
> | Associate Professor              fax:   (617)  632-2444                   |
> | Department of Biostatistics      office: M1B28
> | Harvard School of Public Health  email: rgentlem at jimmy.dfci.harvard.edu   |
> +---------------------------------------------------------------------------+

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

```