[R] Please, remind a function name

Peter Dalgaard p.dalgaard at biostat.ku.dk
Wed May 23 01:56:31 CEST 2007


Vladimir Eremeev 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 <:
Er, what's wrong with

model <- switch(model.type, 
                S  = lm( [s.model formula] ), 
                HK = lm([hk.model formula] ))

?



More information about the R-help mailing list