mercredi 1 janvier 2020

Can't I use many ifelse in else?

This code is a complex if. I write this code because I want to use the flow control to decide whether the user can eat the cake or not. The variables a, b, c, d are some indexes that the user will put in. In this place, I have already set these indexes. Based on the indexes, the nested if will give some responses.

a <- 1 ; b <-  2 ; c <- 3 ; d <- 3

index <- sum(a, b, c, d)

if(index > 11){print("enjoy the cake right now!")
}else{
  if(b == 1 | c == 1){"You don't have the right to eat cake."
  }else{
    ifelse(b == 3, "go to ATM and take money right now", 
           ifelse(b == 2, "use the budget of tomorrow first",print("") )
    )
    print("bb")
    ifelse(c == 4, "run to the cake store and burn some calores ", 
           ifelse(c == 3,"ride youbike to the cake store", 
                  ifelse(c == 2, "ride youbike to the cake store",print(""))))
    print("aa")
    }
        }

My expectation is:

[1] "use the budget of tomorrow first"
[1] "bb"
[1] "ride youbike to the cake store"
[1] "aa"

But the result is:

[1] "bb"
[1] "aa"

Why the R program didn't run the "ifelse" part and just printed "bb", "aa"? Is this because the "else" can't include "ifelse"?

Aucun commentaire:

Enregistrer un commentaire