Learn R Programming

metatools (version 0.2.0)

create_cat_var: Create Categorical Variable from Codelist

Description

Using the grouping from either the decode_var or code_var and a reference variable (ref_var) it will create a categorical variable and the numeric version of that categorical variable.

Usage

create_cat_var(
  data,
  metacore,
  ref_var,
  grp_var,
  num_grp_var = NULL,
  create_from_decode = FALSE,
  strict = TRUE
)

Value

dataset with new column added

Arguments

data

Dataset with reference variable in it

metacore

A metacore object to get the codelist from. If the variable has different codelists for different datasets the metacore object will need to be subsetted using select_dataset from the metacore package.

ref_var

Name of variable to be used as the reference i.e AGE when creating AGEGR1

grp_var

Name of the new grouped variable

num_grp_var

Name of the new numeric decode for the grouped variable. This is optional if no value given no variable will be created

create_from_decode

Sets the decode column of the codelist as the column from which the variable will be created. By default the column is code.

strict

A logical value indicating whether to perform strict checking against the codelist. If TRUE will issue a warning if values in the ref_var column do not fit into the group definitions for the codelist in grp_var. If FALSE no warning is issued and values not defined by the codelist will likely result in NA results.

Examples

Run this code
library(metacore)
library(haven)
library(dplyr)
load(metacore_example("pilot_ADaM.rda"))
spec <- metacore %>% select_dataset("ADSL")
dm <- read_xpt(metatools_example("dm.xpt")) %>%
  select(USUBJID, AGE)
# Grouping Column Only
create_cat_var(dm, spec, AGE, AGEGR1)
# Grouping Column and Numeric Decode
create_cat_var(dm, spec, AGE, AGEGR1, AGEGR1N)

Run the code above in your browser using DataLab