Function Strings

Reference for writing map function strings in CensusMapper.

How Function Strings Work

Each map has a set of variables (e.g. v1, v2, v3) fetched from the backend as census data vectors. The function string is a mathjs expression that transforms those variables into a displayable value per geographic feature.

Variables are referenced directly by their key:

v1 / v2
(v1 + v2) / v3
diversity(c(v1, v2, v3, v4))

The expression is evaluated for each feature. The p object gives access to raw tile feature properties such as geography type and ID.

Built-in mathjs Functions

All standard mathjs functions are available. A selection of commonly useful ones:

FunctionDescription
abs(x)Absolute value
max(a, b, ...)Maximum of values
min(a, b, ...)Minimum of values
sum(a, b, ...)Sum of values
sqrt(x)Square root
square(x)Square (x²)
log(x) / log(x, base)Natural log or log to given base
sign(x)Sign: −1, 0, or 1
round(x) / round(x, n)Round to n decimal places
floor(x) / ceil(x)Floor / ceiling
mean(a, b, ...)Arithmetic mean
median(a, b, ...)Median
std(a, b, ...)Standard deviation
pow(x, y)x to the power y
exp(x)e^x
mod(x, y)Modulo

Conditions and Missing Values

These functions follow R and dplyr, so a function string reads like the inside of a mutate() call. Missing census values are NA, and as in R most operations on NA give NA: regions with an NA result are shown as having no data.

Comparisons use <, <=, >, >=, == and !=; combine them with and, or and not (R's &, | and !).

if_else(condition, true, false)

Returns true if the condition holds, false if it doesn't, and NA if the condition is NA (for example because one of its variables is missing).

if_else(v1 >= 100, v1/v2, NA)   // ratio only where v1 >= 100, blank elsewhere
if_else(v1 > 0 and v2 > 0, 1, 0)
coalesce(x, y, ...)

Returns the first argument that isn't missing — use it to replace NA with a default value.

coalesce(v1 / v2, 0)   // 0 where the ratio is missing
is.na(x)

TRUE if x is missing, FALSE otherwise. Can also be written is_na(x).

if_else(is.na(v1), 0, v1)
c(a, b, ...)

Combines values into a vector, for use with functions such as sum, max and min. Comparisons and is.na work element by element.

sum(c(v1, v2, v3, v4) > 0)   // how many of these variables are positive
max(c(v1, v2, v3))

Maps written with the older ifge(), naz() and countgz() functions, or with 'x' as a “no value” marker, keep working, as do the older forms of gini, quantile, diversity and the other census functions below that take every value as a separate argument.

CensusMapper Custom Functions

These functions are registered specifically for CensusMapper and extend mathjs with census-specific operations.

diversity(c(a, b, ...))

Herfindahl diversity index — measures how evenly distributed values are across categories. Returns a value between 0 (complete concentration) and ~1 (maximum diversity). Formula: 1 − sum(x²) / (sum(x))²

diversity(c(v1, v2, v3, v4))
dissimilarity(c(a1, a2, ...), c(b1, b2, ...))

Index of dissimilarity — measures spatial segregation between two groups, given the counts of group A and group B per category. Formula: 0.5 × sum(|aᵢ/A − bᵢ/B|)

dissimilarity(c(v1, v3), c(v2, v4))
// compares v1, v3 (group A) with v2, v4 (group B)
wasserstein(c(a1, a2, ...), c(b1, b2, ...), p = 1)

Wasserstein (Earth Mover's) distance between two samples of the same size. The optional third argument is the power p.

wasserstein(c(v1, v2, v3), c(v4, v5, v6))
wasserstein(c(v1, v2, v3), c(v4, v5, v6), 2)   // with p=2
gini(c(counts), c(lower_cutoffs), total)

Gini coefficient for income inequality from binned data: the counts per income bracket and each bracket's lower dollar cutoff. The optional total income improves the estimate for the top bracket. Returns a value between 0 (perfect equality) and 1 (maximum inequality).

gini(c(v1, v2, v3, v4, v5), c(0, 10000, 20000, 30000, 50000), v6)
gini2(c(counts), c(lower_cutoffs), total)

Alternative Gini implementation using a different method for estimating bracket midpoints. Same arguments as gini.

binned_quantile(c(counts), c(lower_cutoffs), p)

Quantile from binned data, with the same counts and lower cutoffs as gini; p is the quantile level (0–1).

binned_quantile(c(v1, v2, v3, v4), c(0, 10000, 20000, 50000), 0.5)   // median
binned_quantile(c(v1, v2, v3, v4), c(0, 10000, 20000, 50000), 0.8)   // 80th percentile
mortality(y)

Mortality rate — returns the probability of dying within a 5-year age cohort starting at age y, based on Canadian provincial life tables. Uses the geographic unit's province code to look up province-specific rates.

mortality(25)   // mortality rate for 25–30 age cohort
survival(y)

Survival rate — complement of mortality(y). Returns the probability of surviving the 5-year cohort starting at age y.

survival(25)   // survival rate for 25–30 age cohort
levels(type1, val1, type2, val2, ..., default)

Geography-level switcher — returns a different value depending on the current feature's geography type (e.g. "DA", "CT", "CSD"). Arguments alternate between geography type strings and corresponding values; the last argument is the default if no type matches.

levels("DA", v1/v2, "CT", v3/v4, v5/v6)
// use v1/v2 for DAs, v3/v4 for CTs, v5/v6 otherwise
cdf(x)

Cumulative distribution function (standard normal, upper tail). Computes 0.5 × erf(−x / √2).

cdf(2.0)   // ≈ 0.023

Examples

Simple ratio

v1 / v2

Diversity across categories

diversity(c(v1, v2, v3, v4, v5))

Income Gini from bracket data

gini(c(v1, v2, v3, v4, v5), c(0, 20000, 40000, 60000, 100000), v6)

Geography-adjusted expression

levels("DA", v1/v2, v3/v4)

Replace missing values with 0

coalesce(v1 / v2, 0)

Conditional ratio (only where the base is large enough, blank elsewhere)

if_else(v1 >= 100, v1/v2, NA)

Count the categories with any population

sum(c(v1, v2, v3, v4) > 0)