Uses normalized distinct codes and sparse aggregation. Dots are removed, case is normalized, and surrounding whitespace is trimmed. Prefixes are literal, not regular expressions. Range (`Q20-Q26`), wildcard (` regex and comma/space-separated list notation is rejected with an error rather than silently matching nothing; supply explicit code vectors instead. `*`, `+` and `/` remain literal characters. Codes are not clinically validated. Repeated IDs are combined across rows and columns. Flags are TRUE for any match, FALSE for recorded codes without a match, and NA when all selected codes for an ID are missing/blank. FALSE does not establish disease absence. Input is unchanged. Use thip_filter_icd() to restrict a lazy source, then collect explicitly before local multi-condition flagging.
Usage
icd_flag(data, conditions, id, cols, match = c("exact", "prefix"))Arguments
- data
A local data frame. Lazy tables are rejected, never auto-collected.
- conditions
Nonempty named list of character code vectors. Empty vectors are allowed and never match recorded codes; condition names must be unique.
- id
Name of the ID column to aggregate by (e.g. encounter or entry).
- cols
Character vector naming diagnosis columns (character or factor).
- match
Either "exact" (default) or "prefix".
Value
A data frame with one row per ID, in first-seen order, and logical condition columns. Empty input yields zero rows with the same schema.
Examples
x <- data.frame(id = c("a", "b"), dx = c("E11.9", NA_character_))
icd_flag(x, list(diabetes = "E11"), "id", "dx", match = "prefix")
#> id diabetes
#> 1 a TRUE
#> 2 b NA