ifelse function

The ifelse function checks whether a specific condition is true, and returns the value of iftrue if the condition is true, otherwise it returns the value of iffalse if the condition is false.


Parameters

ifelse(condition, iftrue, iffalse)

Parameter

Description

condition

It is the condition to be satisfied by the function. The condition parameter is mandatory.

iftrue

It is the value or string to be displayed by the function if the condition is verified. The iftrue parameter is mandatory.

iffalse

It is the value or string to be displayed by the function if the condition is not verified. The iffalse parameter is mandatory.


Example

The following example uses the Bike sales dataset.

Description

Screenshot

In this example, we want that if the Country is Canada, the attribute is filled with the corresponding Profit values, otherwise if the conditions are not satisfied the string ‘N/A’ is displayed.

We type the following formula: ifelse($"Country" is 'Canada',$"Profit",'N/A')

As you can see, the attribute has been populated with the corresponding Profit value when the country is Canada, for all the other Country values an ‘N/A’ is displayed.

The None value.

In this example, use cases with the None value in ordinal and nominal attributes, with both the == and is operators.

Description

Screenshot

In this example, we have manually modified the Country and the Profit attributes, so that their first value is empty.

Add two new attributes, one called nom, and one called ord.

In the nom attribute, type the following formula: ifelse($"Country"=="Canada",True,False)

As you can see, the None value has been returned as False, as the None value doesn’t satisfy the condition.

In the ord attribute, type the following formula: ifelse($"Profit" == 590,True,False)

As you can see, the None value has been returned as None, as with ordinal attributes the == returns the None values.

If we replace the == operator with is, for both functions the None value will always be returned as a binary value.