camr_build_demographics_table: Build Demographics Table

View source: R/R05-Statistical_tools.R

camr_build_demographics_tableR Documentation

Build Demographics Table

Description

A function that will build a data frame that is well- formatted to easily create a flextable object.

Usage

camr_build_demographics_table(df, grp, funcs)

Arguments

df

A data frame that contains variables named according to the specification created by Kevin Potter (i.e. XXX.FFF.Var_name), where XXX is a 3-letter subset code and FFF is a 3-letter format identifier.

grp

A variable name that contains the grouping variable for the demographics table.

funcs

A named list of purrr-style lambda functions where the names follow one of 3 conventions (below). Note: for all CHR variables, the function operates on the unique values within that variable, NOT on the variable itself. Further, the value n is available for use within these functions. All functions must return a CHR type.

  • FFFwhich applies the function to all variables (with the exception of IDS.CHR...) with that format identifier.

  • XXX.FFFwhich applies the function to all variables (with the exception of IDS.CHR...) with that subset code and format identifier.

  • XXX.FFF.Var_namewhich applies the function to that variable

Value

A data frame.

Author(s)

William Schmitt

Examples

# Example data frame
df <- data.frame(
  SSS.CHR.Group = c('G1', 'G1', 'G2', 'G2'),
  SBJ.INT.Age = c(45, 47, 48, 52),
  SBJ.CHR.Race = c('White', 'White', 'Asian', 'Black')
)

# Runs function on CHR and INT variables
dt <- camr_build_demographics_table(
  df,
  SSS.CHR.Group,
  list(
    'INT' = ~paste0(round(mean(.), 2)),
    'CHR' = ~paste0(./n*100, '%')
  )
)

# Runs function on SBJ.INT and SBJ.CHR variables
dt <- camr_build_demographics_table(
  df,
  SSS.CHR.Group,
  list(
    'SBJ.INT' = ~paste0(round(mean(.), 2)),
    'SBJ.CHR' = ~paste0(./n*100, '%')
  )
)

# Runs function on individual columns
dt <- camr_build_demographics_table(
  df,
  SSS.CHR.Group,
  list(
    'SBJ.INT.Age' = ~paste0(round(mean(.), 2)),
    'SBJ.CHR.Race' = ~paste0(./n*100, '%')
  )
)

rettopnivek/camrprojects documentation built on March 26, 2024, 9:17 a.m.