6.14 Derived columns with mutate
--mutate is available to eda, er and nca. It changes or adds columns before filters run, writes the transformed dataset into the output folder, and records every assignment.
Ordered assignments
Assignments use = or <- and are separated by semicolons or newlines:
LNDV = log(DV);
WT70 = WT / 70;
EARLY = TIME <= 4;
LABEL = paste0("P", PERIOD)
They run left to right. A later expression may use a column created earlier in the same mutate specification. Assigning to an existing name replaces that column; assigning a new name appends it.
Logical results are written as 1 or 0. Numeric and text expressions are supported. A missing input or invalid mathematical result, such as log(0), writes . and produces a summary warning when a resulting column is missing on rows.
Output and provenance
The transformed file is named <source_stem>_mutated.<extension> in the fresh command output folder. The source remains unchanged. nlmx preserves the detected delimiter, comment lines and line-ending style as far as the mutate writer's supported dialect permits.
The Chapter 6 example creates LNDV, WT70 and PERIOD, then filters to PK observations. The log reports 216 rows mutated before 192 rows remain for EDA. This order matters: the output mutated file contains all 216 source records, while EDA summaries describe the filtered 192.
Command availability
qc-data, autoinit and screen refuse [data] mutate; the program directs the user to mutate through EDA, ER or NCA, or prepare the column in the source workflow. This prevents silent derivation from becoming part of commands whose immediate purpose is source QC or model preparation.
For a formal modelling dataset, consider materialising derived columns with a version-controlled preparation program and using nlmx mutate for transparent exploration. If mutate defines the final analysis data, retain the exact expression, written dataset and input hash.
Expression functions
Mutate uses the grammar in Table 6.3. paste joins arguments with spaces; paste0 joins directly. min/max are row-wise across their arguments, not column summaries. round(x,digits) is scalar. There are no grouped or lag/window operations in this expression language.
Common mistakes
- Assuming assignments run independently rather than in order.
- Interpreting row-wise
max(A,B)as a dataset maximum. - Forgetting that invalid logs become missing.
- Looking at the mutated file's row count as the filtered analysis count.
- Using ad hoc mutate expressions without archiving them.
Checklist
- Assignment order is intentional.
- Existing-column replacements are reviewed.
- Missing results are counted.
- The mutated file and expression are retained.
- Filter counts are distinguished from mutation counts.
Further reading
See the captured txtmutate/filter log and generated csvblq_mutated.csv. Chapter 13 will cover EDA-specific derived variables and custom tables.