prep_add_missing_codes: Insert missing codes for 'NA's based on rules

View source: R/prep_add_missing_codes.R

prep_add_missing_codesR Documentation

Insert missing codes for NAs based on rules

Description

Insert missing codes for NAs based on rules

Usage

prep_add_missing_codes(
  resp_vars,
  study_data,
  meta_data,
  label_col,
  rules,
  use_value_labels,
  overwrite = FALSE
)

Arguments

resp_vars

variable list the name of the measurement variables to be modified, all from rules, if omitted

study_data

data.frame the data frame that contains the measurements

meta_data

data.frame the data frame that contains metadata attributes of study data

label_col

variable attribute the name of the column in the metadata with labels of variables

rules

data.frame with the columns:

  • resp_vars: Variable, whose NA-values should be replaced by jump codes

  • CODE_CLASS: Either MISSING or JUMP: Is the currently described case an expected missing value (JUMP) or not (MISSING)

  • CODE_VALUE: The jump code or missing code

  • CODE_LABEL: A label describing the reason for the missing value

  • RULE: A rule in REDcap style (see, e.g., REDcap help, REDcap how-to), and REDcap branching logic that describes cases for the missing

use_value_labels

logical In rules for factors, use the value labels, not the codes. Defaults to TRUE, if any VALUE_LABELS are given in the metadata.

overwrite

logical Also insert missing codes, if the values are not NA

Value

a list with the entries:

  • ModifiedStudyData: Study data with NAs replaced by the CODE_VALUE

  • ModifiedMetaData: Metadata having the new codes amended in the columns JUMP_LIST or MISSING_LIST, respectively

Examples

## Not run: 
load(system.file("extdata", "study_data.RData", package = "dataquieR"))
load(system.file("extdata", "meta_data.RData", package = "dataquieR"))
vn <- subset(r$ModifiedMetaData, LABEL == "PREGNANT_0", VAR_NAMES)[[1]]
rules <- tibble::tribble(
  ~resp_vars, ~CODE_CLASS, ~CODE_LABEL, ~CODE_VALUE, ~RULE,
  "PREGNANT_0", "JUMP", "No pregnancies in males", "9999", '[SEX_0]=1',
 )
 r <- prep_add_missing_codes(NA, study_data, meta_data,
   label_col = "LABEL", rules, use_value_labels = FALSE)
 subset(r$ModifiedMetaData, LABEL == "PREGNANT_0", JUMP_LIST)
 subset(meta_data, LABEL == "PREGNANT_0", JUMP_LIST)
 table(study_data[[vn]])
 table(r$ModifiedStudyData[[vn]])
 r <- prep_add_missing_codes(NA, study_data, meta_data,
   label_col = "LABEL", rules, use_value_labels = FALSE, overwrite = TRUE)
 table(study_data[[vn]])
 table(r$ModifiedStudyData[[vn]])

rules <- tibble::tribble(
  ~resp_vars, ~CODE_CLASS, ~CODE_LABEL, ~CODE_VALUE, ~RULE,
  "PREGNANT_0", "JUMP", "No pregnancies in males", "9999", '[SEX_0]="males"',
 )
 r <- prep_add_missing_codes(NA, study_data, meta_data,
   label_col = "LABEL", rules, use_value_labels = TRUE, overwrite = FALSE)
 table(study_data[[vn]])
 table(r$ModifiedStudyData[[vn]])

rules <- tibble::tribble(
  ~resp_vars, ~CODE_CLASS, ~CODE_LABEL, ~CODE_VALUE, ~RULE,
  "PREGNANT_0", "JUMP", "No pregs in males", "9999", '[v00002]="males"',
 )
 r <- prep_add_missing_codes(NA, study_data, meta_data,
   label_col = "LABEL", rules, use_value_labels = TRUE, overwrite = FALSE)
 table(study_data[[vn]])
 table(r$ModifiedStudyData[[vn]])
 devtools::load_all(".")

study_data$v00002 <- ifelse(study_data$v00002 == "0", "females", "males")
meta_data[meta_data$LABEL == "SEX_0", "VALUE_LABELS"] <- "females|males"
rules <- tibble::tribble(
  ~resp_vars, ~CODE_CLASS, ~CODE_LABEL, ~CODE_VALUE, ~RULE,
  "PREGNANT_0", "JUMP", "No pregnancies in males", "9999", '[v00002]="males"',
)
r <- prep_add_missing_codes(NA, study_data, meta_data,
                            label_col = "LABEL", rules, use_value_labels = TRUE, overwrite = FALSE)
table(study_data[[vn]])
table(r$ModifiedStudyData[[vn]])

## End(Not run)

dataquieR documentation built on May 29, 2024, 7:18 a.m.