The default method will guess whether ICD-9 or ICD-10 then sort
based on that type. For ICD-10 codes, note that setting short is
unnecessary and ignored. All codes should consistently use the decimal
divider.
sort_icd(x, decreasing = FALSE, short_code = guess_short(x), ...) # S3 method for icd10 sort(x, decreasing = FALSE, ...) # S3 method for icd10cm sort(x, decreasing = FALSE, ...) # S3 method for icd10be sort(x, decreasing = FALSE, ...) # S3 method for icd9 sort(x, decreasing = FALSE, short_code = guess_short(x), ...) order.icd9(x, na.last = TRUE) order.icd10cm(x) order.icd10be(x)
| x | vector of ICD codes to sort or order |
|---|---|
| decreasing | Logical See |
| short_code | single logical value which determines whether the ICD-9
code provided is in short ( |
| ... | arguments passed on to other functions |
| na.last | Logical, analogous to |
For sort, a sorted vector of ICD-9 codes. Numeric, then E codes, then V codes. For order, an integer vector is returned with the order of each code.
Note that sort is an S3 generic, whereas
order is not. Thus we export order.icd10cm, but
not sort.icd10cm, etc..
Sorts lists of numeric, V or E codes. Note that a simple
numeric sort does not work for ICD-9 codes, since 162 > 1620, and
also ‘V’ codes precede ‘E’ codes. Numeric codes are first,
then ‘V’, then ‘E’. A factor is returned if a factor is
given.
There are some codes which are sequenced
out of lexicographic order, e.g., C7A and C7B are between
C80 and C81; D3A is between D48 and D49.
# order ICD-10-CM is not lexicographic: codes <- as.icd10cm(c("C7A", "C79", "C80", "C81", "C7B")) # as the class is set, use S3 dispatch to get the right answer sort(codes)#> [1] "C7A" "C79" "C80" "C81" "C7B"# or call directly, but recall S3 dispatch will only work once 'icd' is # attached using: library(icd) icd:::sort.icd10cm(c("C7A", "C79", "C80", "C81", "C7B"))#> [1] "C7A" "C79" "C80" "C81" "C7B"stopifnot(!identical( order.icd10cm(as.character(codes)), order(codes) )) icd::order.icd9(c("V20", NA, "100", NA, "E998", "101"))#> [1] 3 6 1 5 2 4codes[order.icd10cm(codes)]#> [1] "C7A" "C79" "C80" "C81" "C7B"# Note that base::order does NOT do S3 dispatch, so the following does not work: codes[order(codes)]#> [1] "C7A" "C7B" "C79" "C80" "C81"