Re: [R] Please, remind a function name

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Tue, 22 May 2007 19:39:13 -0400

Note that a significant difference between the two solutions is that the OO solution allows new methods corresponding to "method=" to be added without changing the function holding the statements.

On 5/22/07, Vladimir Eremeev <wl2776_at_gmail.com> wrote:
>
> My solution (after a cup of coffee :) ):
>
> model.formula<-switch(model.type,S=[s-model formula],HK=[hk-model formula])
> model<-lm(model.formula)
>
>
> Gabor Grothendieck wrote:
> >
> > You could use the class dispatching mechanism:
> >
> >
> > mymodel <- function(a, b, method = "S") {
> > .Class <- method
> > NextMethod("mymodel")
> > }
> >
> > mymodel.S <- function(a, b, method = "S") cat("S:", a, b, method, "\n")
> > mymodel.HK <- function(a, b, method = "S") cat("HK:", a, b, method, "\n")
> >
> > mymodel(1:3, 1:4)
> > mymodel(1:3, 1:4, "S")
> > mymodel(1:3, 1:4, "HK")
> >
> >
> >
> > On 5/22/07, Vladimir Eremeev <wl2776_at_gmail.com> wrote:
> >>
> >> Sorry, I'm stuck. :)
> >>
> >> I am writing a function, which would fit either one linear model or
> >> another
> >> one, depending on its argument model.type.
> >> And I don't want to use several if's, because R allows doing it with much
> >> more beauty.
> >>
> >> That is I am looking for prettier alternative to the following
> >>
> >> AB2C<-function(a,b,model.type="S") { # or HK, and return NULL if this
> >> argument has any other value
> >> [ do some stuff]
> >>
> >> if(model.type=="S") model<-lm( [s.model formula] )
> >> else if (model.type == "HK") model<-lm([hk.model formula])
> >> else model<-NULL
> >>
> >> return(model)
> >> }
> >>
> >> I was looking at "switch", but didn't come to solution. Sorry <:)
> >
> >
>
> --
> View this message in context: http://www.nabble.com/Please%2C-remind-a-function-name-tf3797444.html#a10750855
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help_at_stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



R-help_at_stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Tue 22 May 2007 - 23:43:27 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 23 May 2007 - 10:31:15 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.