Documentation

13.5 Mutate recipes and unit conversions

Mutation is most useful when it makes a scientific definition explicit and reviewable. Keep each expression simple, label resulting units, and compare selected rows against an independent calculation.

Common recipes

Body mass index, when WT is kg and HT is cm:

BMI = WT / (HT / 100)^2

Convert concentration from mg/L to ng/mL (numerically equivalent, but document the source units):

DV = DV * 1000

Create a natural-log observation while retaining DV:

LNDV = log(DV)

Create a dose-normalized observation only when dose is positive:

DV_PER_MG = ifelse(DOSE > 0, DV / DOSE, NA)

The language represents missing results as ., and the exact spelling of a missing constant should be tested against the installed implementation. Often the built-in DVNORM derived column is preferable because it follows EDA's dose history.

Time after dose

eda derives TAD from dosing records and uses a supplied --tad-col when available. A row-wise mutation cannot reliably reconstruct full dosing history with ADDL, II, SS, or multiple doses, because that requires state across records. Use the built-in TAD logic or a validated upstream derivation; do not imitate it with TIME - fixed_value except for a deliberately simple design.

Categories and flags

HEAVY = WT > 75; AGE_GROUP = ifelse(AGE < 65, "<65", ">=65")

Flags are numeric 0/1. Text categories should be reviewed for quoting, capitalization, and missing values. Avoid data-dependent cut points chosen after viewing the outcome; predefine clinically meaningful groups when possible.

Common mistakes

  • Using a correct formula with incorrect source units.
  • Overwriting DV when both original and converted values are needed.
  • Reconstructing longitudinal quantities with a row-only formula.
  • Treating derived categories as natural biological boundaries.

Checklist

  • State source and result units.
  • Keep original columns when audit requires them.
  • Verify a few rows independently.
  • Review invalid-domain and missing counts.
  • Prefer built-in longitudinal derivations for dose history.

Further reading

See mutation grammar, derived EDA columns, and derived columns with mutate.