
Recalculate and Verify Calculated Fields in REDCap Data
Source:R/rd_recalculate.R
rd_recalculate.RdRecalculates fields marked as calculated in the REDCap dictionary, compares them with the original values, and reports discrepancies. If any differences are found, new recalculated fields are added to the dataset and dictionary with _recalc appended to the names.
Arguments
- project
A list containing the REDCap data, dictionary, and event mapping (expected
redcap_data()output). Overridesdata,dic, andevent_form.- data
A
data.frameortibblewith the REDCap dataset.- dic
A
data.framewith the REDCap dictionary.- event_form
Only applicable for longitudinal projects (presence of events). Event-to-form mapping for longitudinal projects.
- exclude
Optional. Character vector of field names to exclude from recalculation.
Value
A list with:
- data
The dataset with new
_recalcfields for any differing calculated fields.- dictionary
Updated dictionary including the new
_recalcfields.- event_form
The
event_formpassed in (if applicable).- results
Summary report of the recalculation process, including tables of discrepancies.
Details
Fields of type
calcin the dictionary are recalculated. As in REDCap, a calculation without a numeric result (e.g. a division by zero) is blank (NA).Recalculated values are compared with the original values, both rounded to three decimals (e.g. 1.0004 and 1 are equal).
If differences exist, new fields
[field_name]_recalcare added to the dataset and dictionary, with the label of the original field followed by(Recalculated). Running the function again on its output updates these fields instead of duplicating them.The fields added by this function are recognised because they are calculated fields with the same calculation as the original field and, when the dictionary has the column
field_label, its label followed by(Recalculated)(or by(Recalculate), the label of previous versions of the package, which are also recognised). No other field or column is overwritten: if[field_name]_recalcis already one of them, the recalculated values are saved in[field_name]_recalc_1(or_2, ...) with a warning.Works for single-event projects; for longitudinal projects,
event_formmust be provided.The calculation of each field is translated to R with
rd_rlogic(): calculations that can't be translated (e.g. with smart variables or functions that it doesn't know) are not recalculated, andTranscribed?isNo.The original and recalculated values are compared as numbers when both are numbers, also written as text (e.g.
'100000', the result ofif()with texts) or asTRUE/FALSE(the 1/0 of a condition), rounded to 3 decimals: the comparison doesn't depend on how R writes the numbers.A calculated field of the dictionary that isn't in the data can't be compared: a warning is shown, and its
Is equal?andDifferent valuesareNA.Existing generated columns are refreshed even when the original values now agree with the calculation. If a formula cannot be translated or evaluated, its previously generated column is set to
NAwith a warning. Excluded fields and their generated columns are left unchanged.Unsupported formulas are reported as non-transcribed in
results; they do not stop the recalculation of other fields. Restrictions on repeated-source references are those ofrd_rlogic().
See also
Other process functions:
rd_checkbox(),
rd_dates(),
rd_delete_vars(),
rd_dictionary(),
rd_factor(),
rd_insert_na(),
rd_rlogic(),
rd_split(),
rd_transform()
Examples
# Recalculate all calculated fields
if (FALSE) { # \dontrun{
results <- rd_recalculate(
data = covican$data,
dic = covican$dictionary,
event_form = covican$event_form
)
} # }
# Recalculate but exclude some variables
if (FALSE) { # \dontrun{
results <- rd_recalculate(
project = covican,
exclude = c("age", "screening_fail_crit")
)
} # }