Documentation

13A.7 Distinct, sort, head, and select

These four steps control row duplication, order, sample size, and visible columns. They run after mutation and filtering, in the fixed order distinct → sort → head → select.

--distinct ID keeps the first row for every ID. Several columns define a combination, for example --distinct ID,OCC. This is appropriate when baseline variables are repeated identically on records, but unsafe when the first row can contain a different time-varying value. distinct is not an aggregation: it discards later rows rather than calculating a mean or selecting a statistically representative value.

--sort ID,-TIME orders ascending ID and descending TIME. A leading hyphen reverses one column. Because shared sort runs after distinct, it cannot choose which duplicate survives. Prepare source order or use an explicit upstream rule when survival matters.

--head 20 retains the first 20 rows after sorting. It is excellent for a compact listing and dangerous as an analysis sampling method because it is deterministic and order-dependent, not random.

--select ID,TIME,DV retains columns in the requested order. It runs last, so earlier expressions and sorts can still use omitted variables. Listing tables respect the final selection; plots and other tables fail if item-required columns were removed globally.

Item-specific examples

A repeated-record diagnostic pack can put distinct = ["ID"] inside its demographics table and ETA histogram, while leaving concentration profiles untouched. A listing may use sort = ["ID", "TIME"], head = 25, and select without altering any other item.

Common mistakes

  • Treating distinct as a summary or deduplication validation.
  • Believing sort chooses the survivor after distinct.
  • Reporting head rows as a random sample.
  • Selecting away variables required by item-specific filters.

Checklist

  • Verify repeated values are consistent before subject-level distinct.
  • Confirm source order when the first row matters.
  • Use head for inspection, not inference.
  • Keep needed columns through all earlier steps.
  • Review item row counts in custom_summary.json.

Further reading

See fixed order, listing tables, demographics, and settings blocks.