# Expressions

> Every function, and how to refer to variables and cells.

*Source:* https://recense.ai/docs/expressions

## The basics

An expression is a function name with what it applies to in parentheses, combined with arithmetic where you need it.

- Function names ignore case: MEAN, Mean and mean are the same function.
- Some functions have other names, such as avg for mean; each function below lists its own.
- While you type, the editor suggests the functions and variables that fit, shows the arguments a function takes, and offers a fix for a mistake. fx (⌘/) opens the function browser.

*Examples:*

```text
top2box(satisfaction)
mean(q1, q2, q3)
[Evening] - [Morning]
```

## Referring to things

- A variable by its name: satisfaction. A question group by its name; a function that takes several variables reads all of its members.
- Brackets mean different things in different places: in a formula cell they name a cell; in a table pill they choose parts of a group.
- In a formula cell, a cell by its row and column labels: [Hunger, Morning]. In a formula column, [Morning] is this row's Morning cell; in a formula row, [Hunger] is this column's Hunger cell.
- A cell in another table: Table 2[Hunger, Morning].
- A range of cells, as a function argument: mean([Hunger:Convenience, Morning]).
- A label containing , : [ ] " or $ goes in quotes: ["Yes, often", Morning]. $ before a part keeps it in place when a formula is filled.
- When two rows share a label, the editor offers each by its identity, {…}, so the formula stays on the row you meant.

## Operators

- **+  -  *  /** — Arithmetic, with * and / before + and -. Works everywhere.
- **( )** — Group a calculation. Works everywhere.
- **%** — Remainder. Formula cells.
- **==  !=  <  <=  >  >=** — Compare a value, as in age >= 18. Table pills and derived variables.
- **and  or  not** — Combine conditions; & and | mean and and or. Table pills and derived variables.

## Scores

Turn answers into scores: box scores, ranks, standard scores and transforms.

- **top2box(source, max=5)** — Flag the top two response values as 1, others 0. Works in table pills and derived variables.
- **rank(source, pct=false, ties="Average")** — Per-row ascending rank with a choice of tie convention (default average). Works in table pills and derived variables.
- **math_transform(source, kind=…)** — Row-wise log / exp / logit applied to a numeric source. Works in table pills and derived variables.
- **reverse_likert(source, max=…)** — Reverse a Likert-coded source on a 1..=max scale. Works in table pills and derived variables. Also written reverse.
- **zscore(source)** — Per-row z-score, optionally grouped by a categorical variable. Works in table pills and derived variables.
- **box_score(source, n=2, direction="Top")** — Flag the top or bottom N value-label codes of an ordinal source as 1. Works in table pills and derived variables.
- **percentile(source, position, a, [b])** — Per-respondent 0/1 flag based on the percentile rank of the numeric source. Works in table pills and derived variables. Also written pctl.
- **quartile(source)** — Cut a numeric column into four equal-population bands (Q1..Q4) by percentile rank. Works in table pills and derived variables.
- **quintile(source)** — Cut a numeric column into five equal-population bands (Quintile 1..5) by percentile rank. Works in table pills and derived variables.

## Recode

Regroup or rebuild values: bands, recodes and conditional assignments.

- **bin(source, bands…)** — Cut a numeric column into ordered bands (cut-point list or labelled-arm form). Works in table pills and derived variables.
- **recode(source, arms…)** — Map source codes to labelled buckets (list form or labelled-arm form). Works in table pills and derived variables.
- **cast_copy(source, target_type=…)** — Duplicate a variable and cast it into a supported target type. Works in table pills and derived variables.
- **coalesce(sources…)** — First non-missing value across N parallel source columns. Works in table pills and derived variables.
- **case_when(cases=…)** — Assign each row the first arm whose predicate is truthy. Works in table pills and derived variables. Also written casewhen.
- **value_range(source, min, max)** — Per-respondent 0/1 flag: 1 iff the source value is within [min, max] inclusive. Works in table pills and derived variables. Also written range.

## Select

Keep or drop respondents and categories.

- **select(source)** — Keep only rows whose value is in `codes`; others become missing. Works in table pills and derived variables.
- **exclude(source, codes=…)** — Drop rows whose value is in `codes` (they become missing); others pass through. Works in table pills and derived variables.
- **eligible_if(expr=…)** — Boolean eligibility mask from a predicate expression. Works in table pills and derived variables. Also written eligible.
- **pattern(source, regex)** — Per-respondent 0/1 flag: 1 iff the source's text (categorical value label or raw text response) matches the regex; nothing-to-match leaves the base, not a No. Works in table pills and derived variables. Also written match.

## Summaries

Summarise several variables per respondent. In a formula cell, the same functions summarise cells.

- **mean(inputs…)** — Row-wise mean across two or more parallel source columns (listwise on missing). Works in table pills, derived variables and formula cells. Also written avg, average.
- **sum(inputs…)** — Row-wise sum across two or more parallel source columns (listwise on missing). Works in table pills, derived variables and formula cells.
- **product(inputs…)** — Row-wise product across two or more parallel source columns (listwise on missing). Works in table pills and derived variables.
- **min(inputs…)** — Row-wise minimum across two or more parallel source columns (listwise on missing). Works in table pills, derived variables and formula cells.
- **max(inputs…)** — Row-wise maximum across two or more parallel source columns (listwise on missing). Works in table pills, derived variables and formula cells.
- **count(source)** — Row count of the source column, broadcast as a constant. Works in table pills and derived variables.

## Combine

Combine variables: nets, interactions and multi-select indicators.

- **interact(left, right)** — Cross-product of two categorical variables. Works in table pills and derived variables.
- **multi_select_to_binary(source)** — Materialise N binary 0/1 indicators from a categorical source (one per option). Works in table pills and derived variables.
- **net(expr=…)** — Boolean mask from a predicate expression, optionally restricted by universe. Works in table pills and derived variables.
- **theme_group_publish(source_variable, themes=…, coding_frame_id=…, coding_frame_name=…, codebook_revision=…, classification_result=…)** — Publish coded text themes as binary indicator variables. Works in table pills and derived variables.

## Analyses

Multivariate analyses that add one variable per respondent, such as a segment.

- **pca(variables…, auto_variance_threshold=0.8, rotation="None", rotation_max_iterations=100, parallel_analysis_reps=0, parallel_analysis_percentile=0.95)** — Principal component analysis. Works in table pills and derived variables.
- **kmeans(variables…, k=…, max_iterations=300, n_init=5, gap_bootstraps=0, stability_resamples=0, seed=42)** — K-means clustering via Lloyd's algorithm. Works in table pills and derived variables.
- **lca(indicators…, classes=…, max_iterations=300, restarts=5, smoothing_alpha=0.01, convergence_threshold=0.000001, seed=42, output="Posteriors", blrt_bootstrap_reps=0)** — Latent Class Analysis via EM. Works in table pills and derived variables.
- **conjoint(variables…, none=false, coding="Effects", iterations=1000, burn_in=500, seed=42, prior_df_offset=5, use_asc=false, output="Raw", thin=1, n_mixture_components=1, dual_response_none=false)** — Choice-based conjoint via Hierarchical Bayes MNL. Works in table pills and derived variables.
- **maxdiff(variables…, mode="Sequential", iterations=10000, burn_in=5000, seed=42, prior_df_offset=5, mh_scale=0.1, response_model="ScaleHeterogeneity", scale_prior_sd=0.75, output="Raw")** — MaxDiff (best-worst scaling) via Hierarchical Bayes MNL. Works in table pills and derived variables.
- **efa(variables…, rotation="Promax", correlation="Auto", parallel_analysis_reps=100, parallel_analysis_percentile=0.95, score_method="Regression", extraction="PrincipalAxis", rotation_max_iterations=100)** — Exploratory factor analysis (principal-axis or maximum-likelihood). Works in table pills and derived variables.
- **drivers(outcome, drivers…, bootstrap_reps=100, shapley_samples=2000)** — Key-driver analysis (correlation/regression/Johnson/Shapley). Works in table pills and derived variables.
- **typing_tool(target, predictors…, l2_penalty=1, min_hit_rate=0.7, max_iter=200, lr=0.01)** — Multinomial-logit classifier (predicts a categorical target). Works in table pills and derived variables.
- **ranking(variables…, iterations=10000, burn_in=5000, seed=42, prior_df_offset=5, mh_scale=0.1)** — Hierarchical Bayes Plackett-Luce ranking analysis. Works in table pills and derived variables.
- **correspondence(variables…, dimensions=2, normalization="Symmetric", mca_matrix="Indicator", adjusted_inertia=false)** — Correspondence Analysis (simple CA / MCA via eigendecomposition). Works in table pills and derived variables. Also written ca.
- **conjoint_lc(variables…, k=…, none=false, coding="Effects", output="Class", max_iterations=100, restarts=10, convergence_threshold=0.000001, seed=42, use_asc=false)** — Latent Class CBC Conjoint via EM. Works in table pills and derived variables.

## Next steps

- **[Build tables and analysis](/docs/tables-and-analysis)** — Use expressions and formula cells in your tables.
- **[Agent recipes](/docs/agent-recipes)** — Ask the agent to write expressions for you.
