apply_macro_dict: Apply DLU/CLU file to the data frame

View source: R/macro_utils.R

apply_macro_dictR Documentation

Apply DLU/CLU file to the data frame

Description

The variable label attribute will be applied to the data frame from the DLU file.

Usage

apply_macro_dict(
  data,
  dlu,
  clu = NULL,
  date_format = "%d/%m/%Y",
  clean_names = TRUE,
  rm_empty = getOption("cctu_rm_empty", default = "both"),
  check_catvar = FALSE
)

Arguments

data

Data frame to be applied.

dlu

Data frame of DLU, see tidy_dlu for the requirements of DLU.

clu

Data frame of CLU, see details for the requirements of CLU.

date_format

Date format to be converted, default is '%d/%m/%Y'.

clean_names

Conver variable name to lower case (default), this will also change the values in the DLU as well. See clean_names for details.

rm_empty

Remove empty "rows", "cols", or "both" (default), or not "none". The remove_blank_rows_cols function will be used to clean the empty rows and/or columns. If the data is large, this will take a long time, should be set to "none" in this case. Use options(cctu_rm_empty = "none") to set global options.

check_catvar

Check values of the category variable (defined in the DLU file) contain any non-numeric values before converting variables to numeric.

Details

Overview

This function first convert the data to a data.table. This is to avoid the variable attributes dropped by base R functions. Then it will use the dlu file to convert the data into corresponding variable types. After the conversion of the data, variable and value label attribute will be created for the variable, see var_lab) and val_lab.

User can use lab2val to conver value labels of the data to values if the value label is desired. If the clean_names is set to TRUE, the data name and the dl/clu will be cleaned, including the question names in the dlu. The cleaned dlu data will be stored in the cctu environment. This will further be used by cttab to populate the missing report, see report_missing. You can change this with set_dlu function, but it will not have any effect on this function, see set_dlu more details.

Please use get_dlu to get the dlu cleaned by apply_macro_dict or use tidy_dlu to clean it, which is the same function used by apply_macro_dict to clean the DLU.

Variable conversion based on DLU type

  • IntegerData: Convert to numeric.

  • Real: Convert to numeric.

  • Category: If there are any non-numeric characters in the variable, no conversion will be performed, otherwise convert to numeric.

  • Date: Convert data date format with as.Date. The date_format will be used to feed the as.Date function during the conversion.

  • Text: Convert to character.

CLU data requirements

The CLU file contains three columns:

  • ShortCode: Variable name of the downloaded data.

  • CatCode: Category values, it represents the numeric code for an item in the CRF.

  • CatValue: Label of the category values, for example.

Value

A data.table object.

See Also

var_lab val_lab tidy_dlu set_dlu data.table clean_names read_data remove_blank_rows_cols lab2val get_dlu

Examples


# Read MACRO data
dt <- read.csv(system.file("extdata", "pilotdata.csv", package="cctu"),
               colClasses = "character")
dlu <- read.csv(system.file("extdata", "pilotdata_dlu.csv", package="cctu"))
clu <- read.csv(system.file("extdata", "pilotdata_clu.csv", package="cctu"))

# Create subjid
dt$subjid <- substr(dt$USUBJID, 8, 11)

df <- apply_macro_dict(dt, dlu = dlu, clu = clu, clean_names = FALSE)

# Following can give you the same dlu file used by apply_macro_dict
dlu <- tidy_dlu(dlu, clean_names = FALSE)

# Extract data from Lab form
lb <- extract_form(df, "Lab")

# Extract screening visit data from lab form
lb_base <- extract_form(df, "Lab", visit = "SCREENING")

shug0131/cctu documentation built on Nov. 10, 2023, 12:03 p.m.