hvtiRutilities

Overview

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.

Main Functions

Installation

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'))

Usage Examples

Automatic Type Conversion

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 Specific Columns

# Skip conversion for specific variables
dta_partial <- r_data_types(dta, skip_vars = c("boolean", "char"))
# boolean and char remain unchanged, others are converted

Control Factor Creation

# 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)

Working with Variable Labels

# 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)]

Generating Survival Data

# 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)

Key Features

Getting Help

License

GPL (>= 3)