Logical functions¶
Logical functions are based on true or false conditions.
The ifelse
function tests whether a specific condition is true, while all the remaining logical functions test whether an attribute corresponds to a specified data type, and can be used on nominal values only.
Note
By default, results are produced as boolean values, consequently all the single values whose data type corresponds to the function return True
, otherwise they return False
.
However, if the optional binary parameter is set to False
, for example isInteger($"Score", False)
, all the single values whose data type corresponds to the function are returned as 1
, otherwise as 0
.
These functions are useful when users need to change the type of attributes, or they want to identify outliers.
The following functions are available:
See also
Since isDate
, isDatetime
, isMonth
, isQuarter
, isTime
and isWeek
are also Date/Time
functions, refer to the date/time function page for the detailed description.
Since isFloat
, isInteger
and isType
are also Data
functions, refer to the data function page for the detailed description.
ifelse¶
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 - ifelse(condition, iftrue, iffalse)
The following example uses the Bike sales dataset.
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.
Tip
The ifelse function uses a comparison as the first member. Comparison operators have some properties when dealing with None
that need to be considered during the use of this function.
In this example, use cases with the None value in ordinal and nominal attributes, with both the
==
andis
operators.However, if we manually modify the Country and the Profit attributes so that their first value is empty, the results change.
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 asFalse
, as theNone
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 asNone
, as with ordinal attributes the==
returns theNone
values.
If we replace the
==
operator withis
, for both functions theNone
value will always be returned as a binary value.
ifNone¶
The ifNone function fills the attribute’s None
values, which satisfy the specified condition, with the desired value. If the condition is not satisfied, the cell is left empty.
Parameters
ifNone(column, condition, iftrue)
Parameter |
Description |
---|---|
column |
The attribute containing the None values. The iffalse parameter is mandatory |
condition |
The condition to be satisfied by the function. The condition parameter is mandatory. |
iftrue |
The value or string to be displayed by the function if the condition is verified.The iftrue parameter is mandatory. |
Example - ifNone(column, condition, iftrue)
The following example uses a simple dataset we have created on our own, using an Empty Source task and filling the values randomly through a Data Manager task.
In this example, we have the Var_3 attribute, which must contain the Var1 values plus the string
B
replacing theNone
values if the values in Var_2 are above 3.Select the Var_3 attribute and type the following formula
ifNone($"Var_1",$"Var_2">3,"B")
This is the final result. As you can see, in Var_3 attribute, row 4, a
B
has been added, as in Var_1, row 4 the cell isNone
and in Var_2, row 4 the value is above 3.