make_commas_long: Convert comma-separated columns to a wide format

View source: R/utils-long.R

make_commas_longR Documentation

Convert comma-separated columns to a wide format

Description

This is a complex function that accepts a column containing multiple comma-separated values and converts it into a long format. This is designed for those columns in the original NSQIP dataset that store multiple values in a single column separated by commas, such as the anesthes_other column; this is not meant for columns that store data in a wide format (such as the reoperation, readmission, and CPT columns).

Usage

make_commas_long(df, cols, variable.name, levels)

Arguments

df

a data.table containing the comma-separated column.

cols

character vectors of column names that contain the comma-separated values.

variable.name

desired output variable name (optional).

levels

levels for factoring the resulting long table (optional).

Details

The cols parameter should be a single character vector of a length equal to the number of possible unique values in the comma-separated column. The contents of the vector are the name of the comma-separated column with "n" appended, where "n" is equal to the number of the column. The symbol should be named the same as the comma-separated column.

For example, in the original NSQIP, anesthes_other is a comma-separated column. It may contain a value like "General,Spinal,MAC/IV". There are 8 possible unique values that may exist in the comma-separated column. So cols is passed a character vector named anesthes_other that is made up of 8 values - "anesthes_other1", "anesthes_other2", etc.

Because the usual method of factoring columns in the dataframe (via conv_factor) is not possible for the new columns generated in this long table (because they are created within this function long after conv_factor has been called), they must be factored here. If a factor output is desired, the levels must be passed as a list to the levels parameter.

Value

data.table

See Also

make_cols_long


dylanrussellmd/nsqipr documentation built on Oct. 13, 2023, 11:01 a.m.