Learn R Programming

cleanepi: Clean and standardize epidemiological data

cleanepi is an R package designed for cleaning, curating, and standardizing epidemiological data. It streamlines various data cleaning tasks that are typically expected when working with datasets in epidemiology.

Key functionalities of cleanepi include:

  1. Removing irregularities: It removes duplicated and empty rows and columns, as well as columns with constant values.

  2. Handling missing values: It replaces missing values with the standard NA format, ensuring consistency and ease of analysis.

  3. Ensuring data integrity: It ensures the uniqueness of uniquely identified columns, thus maintaining data integrity and preventing duplicates.

  4. Date conversion: It offers functionality to convert character columns to Date format under specific conditions, enhancing data uniformity and facilitating temporal analysis. It also offers conversion of numeric values written in letters into numbers.

  5. Standardizing entries: It can standardize column entries into specified formats, promoting consistency across the dataset.

  6. Time span calculation: It calculates the time span between two elements of type Date, providing valuable demographic insights for epidemiological analysis.

cleanepi operates on data frames or similar structures like tibbles, as well as linelist objects commonly used in epidemiological research. It returns the processed data in the same format, ensuring seamless integration into existing workflows. Additionally, it generates a comprehensive report detailing the outcomes of each cleaning task.

cleanepi is developed by the Epiverse-TRACE team at the Medical Research Council The Gambia unit at the London School of Hygiene and Tropical Medicine.

Installation

cleanepi can be installed from CRAN using

install.packages("cleanepi")

The latest development version of cleanepi can be installed from GitHub.

if (!require("pak")) install.packages("pak")
pak::pak("epiverse-trace/cleanepi")
library(cleanepi)

Quick start

The main function in cleanepi is clean_data(), which internally makes call of almost all standard data cleaning functions, such as removal of empty and duplicated rows and columns, replacement of missing values, etc. However, each function can also be called independently to perform a specific task. This mechanism is explained in details in the vignette. Below is typical example of how to use the clean_data() function.

# READING IN THE TEST DATASET
test_data <- readRDS(
  system.file("extdata", "test_df.RDS", package = "cleanepi")
)
# READING IN THE DATA DICTIONARY
test_dictionary <- readRDS(
  system.file("extdata", "test_dictionary.RDS", package = "cleanepi")
)
# SCAN THROUGH THE DATA
scan_res <- cleanepi::scan_data(test_data)
# DEFINING THE CLEANING PARAMETERS
replace_missing_values <- list(target_columns = NULL, na_strings = "-99")
remove_duplicates <- list(target_columns = NULL)
standardize_dates <- list(
  target_columns = NULL,
  error_tolerance = 0.4,
  format = NULL,
  timeframe = as.Date(c("1973-05-29", "2023-05-29")),
  orders = list(
    world_named_months = c("Ybd", "dby"),
    world_digit_months = c("dmy", "Ymd"),
    US_formats = c("Omdy", "YOmd")
  )
)
standardize_subject_ids <- list(
  target_columns = "study_id",
  prefix = "PS",
  suffix = "P2",
  range = c(1, 100),
  nchar = 7
)
remove_constants <- list(cutoff = 1)
standardize_column_names <- list(
  keep = "date.of.admission",
  rename = c(DOB = "dateOfBirth")
)
to_numeric <- list(target_columns = "sex", lang = "en")
# PERFORMING THE DATA CLEANING
cleaned_data <- clean_data(
  data = test_data,
  standardize_column_names = standardize_column_names,
  remove_constants = remove_constants,
  replace_missing_values = replace_missing_values,
  remove_duplicates = remove_duplicates,
  standardize_dates = standardize_dates,
  standardize_subject_ids = standardize_subject_ids,
  to_numeric = to_numeric,
  dictionary = test_dictionary,
  check_date_sequence = NULL
)
#> ℹ Cleaning column names
#> ℹ Replacing missing values with NA
#> ℹ Removing constant columns and empty rows
#> ℹ Removing duplicated rows
#> ℹ No duplicates were found.
#> ℹ Standardizing Date columns
#> ! Detected 8 values that comply with multiple formats and no values that are
#>   outside of the specified time frame.
#> ℹ Enter `print_report(data = dat, "date_standardization")` to access them,
#>   where "dat" is the object used to store the output from this operation.
#> ℹ Checking subject IDs format
#> 
#> ! Detected 0 missing, 0 duplicated, and 3 incorrect subject IDs.
#> ℹ Enter `print_report(data = dat, "incorrect_subject_id")` to access them,
#>   where "dat" is the object used to store the output from this operation.
#> ℹ You can use the `correct_subject_ids()` function to correct them.
#> ℹ Converting the following  column into numeric: sex
#> 
#> ℹ Performing dictionary-based cleaning
# ADD THE DATA SCANNING RESULT TO THE REPORT
cleaned_data <- cleanepi::add_to_report(
  x = cleaned_data,
  key = "scanning_result",
  value = scan_res
)
# DISPLAY THE DATA CLEANING REPORT
print_report(cleaned_data, print = TRUE)

Vignette

browseVignettes("cleanepi")

Lifecycle

This package is currently an experimental, as defined by the RECON software lifecycle. This means that it is functional, but interfaces and functionalities may change over time, testing and documentation may be lacking.

Contributions

Contributions are welcome via pull requests.

Code of Conduct

Please note that the cleanepi project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.

Citing this package

citation("cleanepi")
#> To cite package 'cleanepi' in publications use:
#> 
#>   Mané K, Degoot A, Ahadzie B, Mohammed N, Bah B (2025). _cleanepi:
#>   Clean and Standardize Epidemiological Data_.
#>   doi:10.5281/zenodo.11473985
#>   <https://doi.org/10.5281/zenodo.11473985>,
#>   <https://epiverse-trace.github.io/cleanepi/>.
#> 
#> A BibTeX entry for LaTeX users is
#> 
#>   @Manual{,
#>     title = {cleanepi: Clean and Standardize Epidemiological Data},
#>     author = {Karim Mané and Abdoelnaser Degoot and Bankolé Ahadzie and Nuredin Mohammed and Bubacarr Bah},
#>     year = {2025},
#>     doi = {10.5281/zenodo.11473985},
#>     url = {https://epiverse-trace.github.io/cleanepi/},
#>   }

Copy Link

Version

Install

install.packages('cleanepi')

Monthly Downloads

253

Version

1.1.2

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Bubacarr Bah

Last Published

October 29th, 2025

Functions in cleanepi (1.1.2)

find_duplicates

Identify and return duplicated rows in a data frame or linelist.
date_detect_simple_format

Get format from a simple Date value
date_guess_convert

Guess if a character vector contains Date values, and convert them to date
make_unique_column_names

Make column names unique when duplicated column names are found after the transformation
is_date_sequence_ordered

Check order of a sequence of date-events
date_i_guess_and_convert

Extract date from a character vector
modify_default_params

Update clean_data default argument's values with the user-provided values.
get_appropriate_format

Transform scanning result format into user-chosen format
numbers_only

Detects whether a string contains only numbers or not.
standardize_column_names

Standardize column names of a data frame or line list
scan_in_character

Scan through a character column
remove_duplicates

Remove duplicates
convert_to_numeric

Convert columns into numeric
remove_constants

Remove constant data, including empty rows, empty columns, and columns with constant values.
date_detect_format

Detect a date format with only 1 separator
date_match_format_and_column

Check whether the number of provided formats matches the number of target columns to be standardized.
date_make_format

Build the auto-detected format
date_detect_separator

Detect the special character that is the separator in the date values
date_rescue_lubridate_failures

Find the dates that lubridate couldn't
date_trim_outliers

Trim dates outside of the defined timeframe
date_guess

Try and guess dates from a characters
date_get_part3

Get part3 of date value
get_default_params

Set and return clean_data default parameters
date_get_format

Infer date format from a vector or characters
date_process

Process date variable
detect_misspelled_options

Detect misspelled options in columns to be cleaned
detect_to_numeric_columns

Detect the numeric columns that appears as characters due to the presence of some character values in the column.
date_get_part1

Split a string based on a pattern and return the first element of the resulting vector.
date_get_part2

Get part2 of date value
perform_remove_constants

Remove constant data.
print_misspelled_values

Print the detected misspelled values
dictionary_make_metadata

Make data dictionary for 1 field
%>%

Pipe operator
scan_data

Scan through a data frame and return the proportion of missing, numeric, Date, character, logical values.
timespan

Calculate time span between dates
standardize_dates

Standardize date variables
retrieve_column_names

Get column names
get_target_column_names

Get the names of the columns from which duplicates will be found
replace_with_na

Detect and replace values with NA from a vector
unnest_report

Unnest an element of the data cleaning report
print_report

Generate report from data cleaning operations
replace_missing_values

Replace missing values with NA
tr_

Flag out what message will be translated using the potools package
add_to_dictionary

Add an element to the data dictionary
clean_using_dictionary

Perform dictionary-based cleaning
common_na_strings

Common strings representing missing values
add_to_report

Add an element to the report object
check_date_sequence

Checks whether the order in a sequence of date events is chronological. order.
check_subject_ids

Check whether the subject IDs comply with the expected format. When incorrect IDs are found, the function sends a warning and the user can call the correct_subject_ids function to correct them.
convert_numeric_to_date

Convert numeric to date
correct_subject_ids

Correct the wrong subject IDs based on the user-provided values.
correct_misspelled_values

Correct misspelled values by using approximate string matching techniques to compare them against the expected values.
clean_data

Clean and standardize data
construct_misspelled_report

Build the report for the detected misspelled values during dictionary-based data cleaning operation
date_detect_day_or_month

Detect the appropriate abbreviation for day or month value
cleanepi-package

cleanepi: Clean and Standardize Epidemiological Data
date_detect_complex_format

Detect complex date format
check_subject_ids_oness

Checks the uniqueness in values of the sample IDs column
date_choose_first_good

Choose the first non-missing date from a data frame of dates
date_check_outsiders

Convert and update date values
date_convert

Convert characters to dates
date_check_timeframe

Check date time frame