Data import is one of the most important early tasks inR.Inclinical data management, datamay arrive as CSV files exported from REDCap, Excel spreadsheets from laboratories, monitoring trackers, or system-generated reports. Import must be handled carefully because the way a file is read affects data types, missing values, variable names, and downstream checks.
REDCap commonly exports data as CSV files, and it may also export a data dictionary and syntax files for statistical packages. A CSV file is a plain text file in which values are separated by commas. CSV files are often safer than Excel files for raw data storage because they are simpler and less likely to contain hidden formatting. However, CSV files do not preserve all metadata, such as labels and validation rules, unless those are exported separately. The data manager should therefore keep the REDCap data dictionary and export settings alongside the
data export.
The following example imports a REDCap-style CSV file using read_csv() from the readr package:
library(tidyverse)
library(janitor)
enrollment_data <- read_csv(“data_raw/redcap_enrollment_export_2026-06-01.csv”) |>
clean_names()
The first line loads the tidyverse. The second line loads janitor. The read_csv() function imports the CSV file into R. The clean_names() function standardizes column names. For example, a column named Participant ID may become participant_id. This makes names easier to type in R, but the transformation should be understood and documented.
Excel files are common when receiving data from laboratories or operational teams. The readxl package can import Excel workbooks without requiring Excel itself to be open. The following code imports a laboratory results workbook:
library(readxl)
library(janitor)
lab_data <- read_excel(“data_raw/laboratory_results_2026-06-01.xlsx”, sheet = “Results”) |>
clean_names()
The sheet argument tells R which worksheet to import. If a workbook has multiple sheets, the data managershouldknowwhich sheet isauthoritative. Workbooksmayalsocontainnotes, merged cells, blank rows, summary tables, or formatting that complicates import. For clinical research workflows, teams should encourage laboratories and operational partners to provide structured rectangular files where possible: one row per observation, one column per variable, and clear headers.
Sometimes imported data types are not what the data manager expects. For example, a participant ID such as 00123 may be interpreted as the number 123, causing the leading zeros to disappear. A date may be imported as text if the file contains mixed formats. A yes/no field may be imported as text, numeric code, or logical value. After import, inspection is essential.
The following code shows how to explicitly inspect the imported dataset:
glimpse(enrollment_data)
glimpse() displays the number of rows, number of columns, column names, data types, and example values. It is often the first command to run after import. If a key variable is imported with the wrong type, it is better to discover that immediately rather than after many downstream steps.
The data manager should also preserve raw imported files. If the script imports a file from data_raw, the script should not overwrite that file. Any cleaned version should be written to a separate folder:
write_csv(enrollment_data, “data_clean/enrollment_clean_2026-06-01.csv”)
Writing a cleaned file may be useful, but it should be done thoughtfully. In some workflows, it is better to regenerate cleaned datasets from scripts rather than store many intermediate files. In other workflows, dated outputs are needed for audit or review. The data management plan should define expectations.
| Import source | Common format | R function | Data management considerations |
|---|---|---|---|
| REDCap export | CSV | `read_csv()` | Preserve export date, data dictionary, and export settings |
| Laboratory workbook | XLSX | `read_excel()` | Confirm worksheet, headers, units, and specimen identifiers |
| Monitoring tracker | XLSX or CSV | `read_excel()` or `read_csv()` | Check that status categories are controlled |
| Randomization list | CSV or XLSX | `read_csv()` or `read_excel()` | Protect allocation information and control access |
| External registry | CSV, TSV, or database extract | `read_csv()` or related functions | Check definitions, coding, and data sharing approvals |