jeudi 11 avril 2019

R select case statement

I would like to do something like the following in R:

Example code (from VBA):

Select Case myBoolean
    Case True
        x = 2
        x = x ^ 2
        x = Sin(x)
    Case False
        x = "I like pancakes"
        x = x & " " & "and icecreames!"
    Case Else
        x = "This is not possible."
End Select

I could use if(){}else if(){}else{} but I find this a bit ugly. (I feel it suggests the first case is "normal" and the other cases are "special"; the Select Case statement from VBA feels neutral to each case.)

I looked at case_when and switch but these seem to be functions that always have to return a result (maybe I'm wrong). I only would like to evaluate (multiple!) lines of codes in each case.

Aucun commentaire:

Enregistrer un commentaire