Options

More than 3 argument If condition, eg: if, elseif, elseif, else...

memecross89memecross89 Member Posts: 2 Learner I
edited February 19 in Help
How can I have more than 3 arguments in an if condition when trying to generate a new attribute "Generate Attribute" operator?
Such as: 
if(Age < 2, "Baby" || Age < 12, "Child") 
&& if(Age < 19, "Teen" || Age < 30, "Adult")
&& if(Age <= 65, "Adult" || Age > 65, "Senior")

or if(Age < 2, "Baby" || Age < 12, "Child" || Age < 19, "Teen" || Age < 30, "Adult" || Age <= 65, "Adult" || Age > 65, "Senior")

For a new column name "AgeGroup" based on an existing Attribute "Age" which just has numerical integer values of instances (people's) age...

I'd rather have everything in one Operator, or use the least amount of operators possible to keep things clean!

Best Answers

  • Options
    rjones13rjones13 Member Posts: 165 Unicorn
    Solution Accepted
    Hi @memecross89,

    The way I generally get around this is to nest another if statement, I.e:
    if(condition, true action, if(condition, true action, else))

    Hopefully this should keep it fairly simple for you application.

    Best,

    Roland
  • Options
    memecross89memecross89 Member Posts: 2 Learner I
    Solution Accepted
    @rjones13

    I was able to create a working if condition for my "Generate Attribute" operator.
    Thanks for confirming that "The way I generally get around this is to nest another if statement, I.e:
    if(condition, true action, if(condition, true action, else))"

    works, at first I tried this before I made this thread. But it seems I forgot an else condition at the end which was giving me an error.

    Here it is, adding "NK" at the end fixed my issues:

    if(Age < 2, "Baby", 
    if(Age < 12, "Child",
    if(Age < 19, "Teen",
    if(Age < 30, "Adult",
    if(Age <= 65, "Adult",
    if(Age > 65, "Senior", "NK"))))))
Sign In or Register to comment.