vendredi 24 juillet 2015

Alternative to if-then-else with logical variables in R

Let's say I have a function, which takes three logical arguments and returns a string indicating which of those were set to TRUE:

func_log <- function(option_1, option_2, option_3) {
    if (option_1 && option_2 && option_3) {
        opt <- "all"
    } else {
        if (option_1 && option_2) {
            opt <- "first two"
        } else {
            if (option_1 && option_3) {
                opt <- "first, last"
            } else {
                opt <- "last two"
            }
        }
    }
return(opt)
}

Is there a way to avoid constructing these if-else here? Using switch may be (would be grateful for an example then)? Any other way?

Aucun commentaire:

Enregistrer un commentaire