make_cols_long: Convert columns to a wide format

View source: R/utils-long.R

make_cols_longR Documentation

Convert columns to a wide format

Description

This is a complex function that accepts specified lists of columns and melts them into a long format from a wide format. This is designed for those columns in the original NSQIP dataset that are stored in a wide format, such as the CPT, readmission and reoperation columns; this is not meant for columns that store data in a comma-separated fashion (such as anesthes_other).

Usage

make_cols_long(
  df,
  ...,
  variable.name,
  na.cols,
  removeFALSE,
  reorder = FALSE,
  fn
)

Arguments

df

a data.table containing the columns in a wide format

...

character vectors of column names that will be melted together into single columns.

variable.name

desired output variable name; this is typically the numbering variable (optional)

na.cols

character vector of column names indicating which records should be removed when NA values are present in these columns (optional).

removeFALSE

character vector of a column name indicating which records should be removed when FALSE values are present in this column (optional).

reorder

logical vector indicating whether records should be reordered after removal of NA and/or FALSE records. Will reorder the variable.name column.

fn

a function that can be used for processing the dataset after melting (optional).

Details

The ... parameter accepts multiple character vectors that should each contain the names of columns that will be melted together to create a single column in the long format output. For example, a character vector named "reoperation" will contain the names "reoperation1", "reoperation2", and "reoperation3", indicating these three columns in the original dataset will be melted into a single column called "reoperation" in the long format.

The removeFALSE character vector should only specify a single logical column.

The fn parameter allows an anonymous function to be passed that operates on the melted data frame. It applies this function prior to any NA or FALSE columns have been removed and before the variable.name has been reordered (if desired).

Value

data.table

See Also

make_commas_long


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