[R] ELIF?

Duncan Murdoch murdoch at stats.uwo.ca
Wed Feb 14 18:45:17 CET 2007


On 2/14/2007 11:35 AM, Johannes Graumann wrote:
> Hello,
> 
> Is there an elegant way to implement something like the elif function (e.g.
> python) and prevent multiple if-else contruct concatenation when coding in
> R?

Why not just concatenate, using "else if"?  For example,

if (condition1) {
    action 1
} else if (condition2) {
    action 2
} else if (condition3) {
    action 3
} else {
    default action
}

There is a switch() function if the condition is something that can be 
computed in advance, e.g. from the example on its man page:

  answer <- switch(type,
              mean = mean(x),
              median = median(x),
              trimmed = mean(x, trim = .1))



More information about the R-help mailing list