Learn R Programming

icd9 (version 1.1)

icd9LongToWide: convert ICD data from long to wide format

Description

This is more complicated than reshape or reshape2::dcast allows. This is a reasonably simple solution using built-in functions.

Usage

icd9LongToWide(icd9df, visitId = "visitId", icd9Field = "icd9",
  prefix = "icd_", min.width = 0, aggregate = TRUE, return.df = FALSE)

Arguments

icd9df

data.frame of long-form data, one column for visitId and one for ICD code

visitId

single character, if NULL will use "visitId" if exists, otherwise the first column

icd9Field

single character string with name of column containing the ICD codes. If left as NULL, the field is guessed to be the first matching ICD or icd, and a warning is given if multiple columns match.

prefix

character, default "icd_" to prefix new columns

min.width,

single integer, if specified, writes out this many columns even if no patients have that many codes. Must be greater than or equal to the maximum number of codes per patient.

aggregate

single logical value, if TRUE (the default) will take more time to find out-of-order visitIds, and combine all the codes for each unique visitId. If FALSE, then out-of-order visitIds will result in a row in the output data per contiguous block of identical visitIds.

return.df

single logical value, if TRUE, return a data frame with a field for the visitId. This may be more convenient, but the default of FALSE gives the more natural return data of a matrix with rownames being the visitIds.

See Also

Other ICD-9 convert: convert, convert, icd9DecimalToParts, icd9DecimalToShort, icd9GetMajor, icd9MajMinToCode, icd9MajMinToDecimal, icd9MajMinToParts, icd9MajMinToShort, icd9PartsToDecimal, icd9PartsToShort, icd9ShortToDecimal, icd9ShortToParts; icd9ChaptersToMap; icd9DropLeadingZeroes, icd9DropLeadingZeroesDecimal, icd9DropLeadingZeroesMajor, icd9DropLeadingZeroesShort; icd9WideToLong

Examples

Run this code
# NOT RUN {
longdf <- data.frame(visitId = c("a", "b", "b", "c"),
    icd9 = c("441", "4424", "443", "441"))
  icd9LongToWide(longdf)
  icd9LongToWide(longdf, prefix = "ICD10_")
# }

Run the code above in your browser using DataLab