- The main condition operators are as follows:
1. Mathematical operators and formulas
(brandA + brandB + brandC) / daysInYear
Math.abs(x)- Computes the absolute values of x (i.e. removes any negative sign).
Math.floor(x)– Rounds x down to the nearest whole number.
Math.ceil(x)– Rounds x up to the nearest whole number.
Math.max(x)– Finds the maximum value from x.
Math.min(x)– Finds the minimum value from x.
2. Boolean expression
A Boolean expression is an expression which evaluates to true or false. Results that are true are returned as values of 1 and false as 0 (i.e., this is a way to construct a binary variable).
For example, we have two numeric variables, v1 and v2, and a text variable, v3:
v1 != v2returns a 1 for observations where v1 and v2 are different, and a value of 0 when they are the same.
isNaN(v1)returns a 1 for observations in v1 that have the value of NaN (missing data), otherwise it returns a 0. For text variables, use
v1 == ""
(v1 + v2) > 0returns a 1 if the sum of v1 and v2 is greater than 0, otherwise it returns a 0.
- The below formula returns a 1 if v3 contains an @ character, otherwise it returns a 0. The indexOf function finds the position of the specified string, whereby -1 is equivalent to being missing and 0 means it is the first character. Note, we have to first define v1 in order to append the indexOf function to it:
var v = v3;
3. Using if...else
The if then else structure is one of the most common methods for writing conditions in programming languages. The following are examples of how this can be used in Displayr.
if (sumCola < 5) 1;
else if (sumCola >= 5 && sumCola <= 10) 2;
else if (sumCola > 10) 3;
We would then need to change Properties > Structure to Nominal and assign our segment labels via DATA VALUES > Labels.
The same example would look like this:
if (sumCola < 5) "Light Drinker"; else if (sumCola >= 5 && sumCola <= 10) "Medium Drinker"; else if (sumCola > 10) "Heavy Drinker"; else "";
Consider a situation where we’ve collected data for four countries: UK, France, Australia, and Japan. The data for each country was held in 4 separate data files and merged into a final file. Membership to a particular segment (market) is defined by a variable called
country. Each country’s data file had a specific weighting variable, so we now have four different weighting variables in the merged data file (
if (country == 1) weight_UK; else if (country == 2) weight_FR; else if (country == 3) weight_AU; else if (country == 4) weight_JP; else NaN;
4. The if...else shortcut
There is also a shortcut method of if...else that lets you write a condition in a single line. In the below example, the formula will return a Yes if x is greater than 1, otherwise a No:
x > 1 ? "Yes" : "No";