Apply DLU/CLU file to the data frame
apply_macro_dict.Rd
The variable label attribute will be applied to the data frame from the DLU file.
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 that will be tried by
as.POSIXct
, default isc("%d/%m/%Y", "%Y-%m-%d", "%Y/%m/%d")
. You can add other formats you may want to try. The conversion will be skipped if the formatting does not succeed.- clean_names
Convert 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"
. Theremove_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. Useoptions(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.POSIXct
. Thedate_format
will be used to feed theas.POSIXct
function during the conversion.Text: Convert to character.
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")