check_valid_codes2()

check_valid_codes2()

check_valid_codes2(data, dump_file, *arg_code_pairs)

Advanced version of check_valid_codes(), available in Datavyu version 1.3.5 and higher.

Can check codes using patterns and can operate over multiple columns. Backwards-compatible with check_valid_codes() so this function should be able to replace calls to check_valid_codes().

Argument Type Description
data String, RVariable, or Hash When this parameter is a String or a column object from getVariable(), the function operates on codes within this column. If the parameter is a Hash (associative array), the function ignores the arg_code_pairs arguments and uses data from this Hash. The Hash must be structured as a nested mapping from columns (either as Strings or RVariables) to Hashes. These nested hashes must be mappings from code names (as Strings) to valid code values (as either lists (Arrays) or patterns (Regexp)).
dump_file String, or Ruby File object Path of the file to dump output to. Use empty String (i.e. “”) to write to the console. You can also specify a Ruby File object (e.g. from File.open()).
*arg_code_pairs Key-value pairs List of code names and valid values, in the format “code_name”, [“valid1”, “valid2”], “code_name2”, [“valid3”, “valid4”], etc. This is ignored if first argument is a Hash.

Returns

Nothing. Generated messages are output to console and/or file.

Example

The following example checks three columns for valid code values. Before the call to the function, a nested mapping is created for each column. The inner map is a mapping from the names of codes to their valid values.

## Params
date_format = /\A\d{2}\/\d{2}\/\d{4}\Z/ # dates must be formatted: ##/##/####
# Associative mapping from column names to mappings from code names to valid values
map = {
  'id' => {
    'testdate' => date_format,
    'idnum' => /\A\d{3}\Z/, # id number must be exactly 3 digits
    'gender' => ['m', 'f', '.'], # gender can be one of 3 values
    'birthdate' => date_format
  },
  'condition' => {
    'cond_ab' => ['a', 'b'] # condition can be either 'a' or 'b'
  },
  'trial' => {
    'trialnum' => /\A\d+\Z/, # trial number must be one or more digits
    'result_xyz' => ['x', 'y', 'z'] # result must be one of 3 values
  }
}

## Body
check_valid_codes2(map, '~/Desktop/check.txt')