hvtiRutilities provides utility functions for working with clinical research data at the Cleveland Clinic Heart, Vascular and Thoracic Institute (HVTI) Clinical Outcomes Registries and Research (CORR) department. The package simplifies common data preparation tasks when working with SAS datasets in R.
r_data_types(): Automatically infer and convert data types in a dataset
proc_contents(): SAS PROC CONTENTS in R
order = "varnum" gives creation orderLen, Pos, and Informat, which haven cannot recover from a .sas7bdat
proc_means(): SAS PROC MEANS in R
n, nmiss, mean, std, min, max, median, q1, q3, pNN, ...), defaulting to SAS's five plus the group's p15, median and p85
class = stratification, dropping missing class levels as SAS doesQNTLDEF=5 definition, not R's defaultlabel_map(): Extract variable labels from labeled datasets
key (variable name) and label columnssample_data(): Generate sample datasets for testing
generate_survival_data(): Simulate a cardiac surgery survival cohort
haven and label_map()
hvtiRutilities is part of the HVTI R family. Install it with the rest of the family through hvtiR:
# install.packages("pak")
pak::pak("ehrlinger/hvtiR")
hvtiR::install()
hvtiR::install() installs every member in one call, so the versions the
members require of one another resolve together. Update later with
hvtiR::update(), from a fresh R session: it will not overwrite a package
that is already loaded.
To install hvtiRutilities on its own:
# pak::pak("ehrlinger/hvtiRutilities")
install.packages("hvtiRutilities", repos = c('https://ehrlinger.r-universe.dev', 'https://cloud.r-project.org'))
library(hvtiRutilities)
# Create sample data
dta <- sample_data(n = 100)
# Examine original types
str(dta)
# boolean: int (values: 1, 2)
# logical: chr (values: "F", "T")
# char: chr (values: "male", "female")
# Apply automatic type conversion
dta_converted <- r_data_types(dta)
# Examine converted types
str(dta_converted)
# boolean: logi (binary 1/2 → TRUE/FALSE)
# logical: Factor (character → factor)
# char: Factor (character → factor)
# Skip conversion for specific variables
dta_partial <- r_data_types(dta, skip_vars = c("boolean", "char"))
# boolean and char remain unchanged, others are converted
# Convert only variables with fewer than 5 unique values to factors
dta_strict <- r_data_types(dta, factor_size = 5)
# Keep binary variables as factors instead of logical
dta_factors <- r_data_types(dta, binary_factor = TRUE)
# Create labeled data (common with SAS imports)
library(labelled)
dta <- data.frame(
age = c(25, 30, 35),
sex = c("M", "F", "M"),
bp = c(120, 130, 125)
)
var_label(dta$age) <- "Patient Age (years)"
var_label(dta$sex) <- "Patient Sex"
var_label(dta$bp) <- "Systolic Blood Pressure (mmHg)"
# Extract labels as a lookup table
labels <- label_map(dta)
print(labels)
# key label
# 1 age Patient Age (years)
# 2 sex Patient Sex
# 3 bp Systolic Blood Pressure (mmHg)
# Use for matching/joining
summary_table <- data.frame(variable = c("age", "bp"))
summary_table$label <- labels$label[match(summary_table$variable, labels$key)]
# Simulate a cardiac surgery cohort (reproducible)
dta <- generate_survival_data(n = 500, seed = 1024)
# Event and reoperation rates
mean(dta$dead) # ~death rate
mean(dta$reop) # ~reoperation rate
# Integrate with the rest of the package
model_data <- r_data_types(
dta,
factor_size = 5,
skip_vars = c("ccfid", "iv_dead", "iv_reop")
)
# Extract variable labels for reporting
lmap <- label_map(model_data)
?r_data_types, ?label_map, ?proc_contents, ?proc_means
vignette("hvtiRutilities"), vignette("sas-procedures"), vignette("dataset-versioning")
vignette("registered-datasets") explains, for analysts, what registering a dataset does, why
the parquet files are named <name>_YYYYMMDD_rN.parquet, which version a job reads, and how a report says sohvtiRutilities.news() in RGPL (>= 3)